Alex Rivera | Logout

2GB limit on file size when using fwrite in C?

Asked 2009-04-08T16:09:38.097
23

I have a short C program that writes into a file until there is no more space on disk:

#include <stdio.h>

int main(void) {
  char c[] = "abcdefghij";
  size_t rez;
  FILE *f = fopen("filldisk.dat", "wb");
  while (1) {
    rez = fwrite(c, 1, sizeof(c), f);
    if (!rez) break;
  }
  fclose(f);
  return 0;
}

When I run the program (in Linux), it stops when the file reaches 2GB.

Is there an internal limitation, due to the FILE structure, or something?

Thanks.

Edit
Report

1 Answer

28

On a 32 bits system (i.e. the OS is 32 bits), by default, fopen and co are limited to 32 bits size/offset/etc... You need to enable the large file support, or use the *64 bits option:

http://www.gnu.org/software/libc/manual/html_node/Opening-Streams.html#index-fopen64-931

Then your fs needs to support this, but except fat and other primitive fs, all of them support creating files > 2 gb.

answered 2009-04-08T16:16:41.950

Your Answer