fseek 现在支持大文件

发布于 2024-11-19 03:15:34 字数 1024 浏览 3 评论 0原文

看来 fseek 现在,至少在我的实现中,自然支持大文件,而无需 fseek64、lseek 或一些奇怪的编译器宏。

这是什么时候发生的事?

#include <cstdio>
#include <cstdlib>
void writeF(const char*fname,size_t nItems){
  FILE *fp=NULL;
  if(NULL==(fp=fopen(fname,"w"))){
    fprintf(stderr,"\t-> problems opening file:%s\n",fname);
    exit(0);
  }
  for(size_t i=0;i<nItems;i++)
    fwrite(&i,sizeof(size_t),1,fp);
  fclose(fp);
}
void getIt(const char *fname,size_t offset,int whence,int nItems){
  size_t ary[nItems];
  FILE *fp = fopen(fname,"r");
  fseek(fp,offset*sizeof(size_t),whence);
  fread(ary,sizeof(size_t),nItems,fp);

  for(int i=0;i<nItems;i++)
    fprintf(stderr,"%lu\n",ary[i]);
  fclose(fp);
}


int main(){
  const char * fname = "temp.bin"; 
  writeF(fname,1000000000);//writefile
  getIt(fname,999999990,SEEK_SET,10);//get last 10 seek from start
  getIt(fname,-10,SEEK_END,10);//get last 10 seek from start
  return 0;
}

上面的代码以二进制 size_t 格式写入一个包含条目 1-10^9 的大文件。 然后写入最后 10 个条目,从文件开头查找,从文件末尾查找。

It appears that fseek now, at least in my implementation, supports large files naturally without fseek64, lseek or some strange compiler macro.

When did this happen?

#include <cstdio>
#include <cstdlib>
void writeF(const char*fname,size_t nItems){
  FILE *fp=NULL;
  if(NULL==(fp=fopen(fname,"w"))){
    fprintf(stderr,"\t-> problems opening file:%s\n",fname);
    exit(0);
  }
  for(size_t i=0;i<nItems;i++)
    fwrite(&i,sizeof(size_t),1,fp);
  fclose(fp);
}
void getIt(const char *fname,size_t offset,int whence,int nItems){
  size_t ary[nItems];
  FILE *fp = fopen(fname,"r");
  fseek(fp,offset*sizeof(size_t),whence);
  fread(ary,sizeof(size_t),nItems,fp);

  for(int i=0;i<nItems;i++)
    fprintf(stderr,"%lu\n",ary[i]);
  fclose(fp);
}


int main(){
  const char * fname = "temp.bin"; 
  writeF(fname,1000000000);//writefile
  getIt(fname,999999990,SEEK_SET,10);//get last 10 seek from start
  getIt(fname,-10,SEEK_END,10);//get last 10 seek from start
  return 0;
}

The code above writes a big file with the entries 1-10^9 in binary size_t format.
And then writes the last 10 entries, seeking from the beginning of the file, and seek from the end of file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

檐上三寸雪 2024-11-26 03:15:34

Linux x86-64 几乎从第一天起就支持大文件(LFS);并且不需要任何特殊的宏等来启用它 - 传统的 fseek()) 和 LFS fseek64() 都已经使用 64 位 off_t

Linux i386(32 位)通常默认为 32 位 off_t,否则会破坏大量应用程序 - 但您可以通过检查 _FILE_OFFSET_BITS 的值来测试环境中定义的内容宏。

请参阅 http://www.suse.de/~aj/linux_lfs.html 了解有关 Linux 大文件支持的完整详细信息。

Linux x86-64 has had large file support (LFS) from pretty much day one; and doesn't require any special macros etc to enable it - both traditional fseek()) and LFS fseek64() already use a 64bit off_t.

Linux i386 (32bit) typically defaults to 32-bit off_t as otherwise it would break a huge number of applications - but you can test what is defined in your environment by checking the value of the _FILE_OFFSET_BITS macro.

See http://www.suse.de/~aj/linux_lfs.html for full details on Linux large file support.

暮年慕年 2024-11-26 03:15:34

签名的

int fseek ( FILE * stream, long int offset, int origin );

范围取决于long的大小。

在某些系统上它是 32 位的,并且您会遇到大文件的问题,而在其他系统上它是 64 位的。

The signature is

int fseek ( FILE * stream, long int offset, int origin );

so the range depends on the size of long.

On some systems it is 32-bit, and you have a problem with large files, on other systems it is 64-bit.

冰魂雪魄 2024-11-26 03:15:34

999999990 是一个普通的 int,完全适合 32 位。但我不相信你能逃脱惩罚:

getIt(fname,99999999990LL,SEEK_SET,10);

999999990 is a normal int and fits perfectly into 32 bits. I don't believe that you'd get away with this though:

getIt(fname,99999999990LL,SEEK_SET,10);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文