简体   繁体   中英

C working with big files, fseek and fread

I am writing a program where it can happen that i need to access files bigger than ~2GB. Would just using fseek and fread without ftell work for bigger files?

int main() {
  unsigned long long int len;
  unsigned char *buffer; /* i know its not initialized */
  unsigned char *sbuffer = buffer;

  FILE *fp = fopen("test123", "rb");
  fread(fp, 8, 1, (void*) &len);

  while (len >= LONG_MAX) {
    fread(fp, 1, LONG_MAX, (void*) sbuffer);
    sbuffer += LONG_MAX;
    len -= LONG_MAX;
  }
  if (len) {
    fread(fp, 1, len, (void*) sbuffer);
  }
}

You can either use the 64 bit variants of fread64 , fseek64 , etc. or you can use #define _FILE_OFFSET_BITS 64 before you include the headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM