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.
source share