fseek 现在支持大文件
看来 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linux x86-64 几乎从第一天起就支持大文件(LFS);并且不需要任何特殊的宏等来启用它 - 传统的
fseek()
) 和 LFSfseek64()
都已经使用 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 LFSfseek64()
already use a 64bitoff_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.
签名的
范围取决于
long
的大小。在某些系统上它是 32 位的,并且您会遇到大文件的问题,而在其他系统上它是 64 位的。
The signature is
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.
999999990
是一个普通的 int,完全适合 32 位。但我不相信你能逃脱惩罚:999999990
is a normal int and fits perfectly into 32 bits. I don't believe that you'd get away with this though: