Fseek now supports large files

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

When did it 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; } 

In the above code, a large file is written with entries 1-10 ^ 9 in binary format size_t. And then it records the last 10 records, starting from the beginning of the file and searches from the end of the file.

+4
source share
3 answers

Linux x86-64 had a lot of file support (LFS) for almost one day; and does not require any special macros, etc., to enable it - both traditional fseek() ) and LFS fseek64() already use 64-bit off_t .

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

For more information on supporting large Linux files, see http://www.suse.de/~aj/linux_lfs.html .

+1
source

Signature

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

therefore, the range depends on the size of long .

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

+1
source

999999990 is a normal int and fits perfectly in 32 bits. I do not believe that you will succeed, though:

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

All Articles