views:

2850

answers:

4

Hi,

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.

+2  A: 

Which file system is your hard drive formatted as?

eduffy
Linux says "vfat", which is FAT32 I think. It is a 4GB flash drive, which is initially (almost) empty.
Gabi
vfat (FAT32) has a limit of 2GB per file anyways, it doesn't matter what you use to code it.
Mike Cooper
Isn't it 4GB on FAT32?
Gabi
http://support.microsoft.com/kb/314463 XP has a FAT filesize limit of 4GB-1B. I thought Linux's vfat would limit at 2GB but reading the source code now I think I was wrong.
ephemient
My rule of thumb for portable access to files on removable media is to limit the file to just under 2GB, which is FAT16's limit because the documentation is vague about whether the size entry is unsigned or signed. Unfortunately, there are a fair number of incorrect apps out there.
RBerteig
+10  A: 

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.

David Cournapeau
A: 

are you writing a virus or what?

mspmsp
I think this is a test program, either to isolate a possible bug in a larger program or just to play around with the language. Besides, this doesn't answer his question.
Chris Lutz
I was joking. It had to be said. I mean, other than striping your drive to overwrite previously used locations, there's not a ton of uses for completely filling a drive. In some OSes you might actually see bad things happen because of virtual memory issues. There can be no ha. :)
mspmsp
+1  A: 

it stops when the file reaches 2GB.

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

This is due to the libc (the standard C library), which by default on a x86 (IA-32) Linux system is 32-bit functions provided by glibc (GNU's C Library). So by default the file stream size is based upon 32-bits -- 2^(32-1).

For using Large File Support, see the web page.

#define _FILE_OFFSET_BITS  64
#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 < sizeof(c) ) { break; }
  }
  fclose(f);
  return 0;
}

Note: Most systems expect fopen (and off_t) to be based on 2^31 file size limit. Replacing them with off64_t and fopen64 makes this explicit, and depending on usage might be best way to go.

mctylr