Click to See Complete Forum and Search --> : Limits for fwrite


spring
February 3rd, 2004, 01:03 PM
I want to write a large file. I keep content to be written in char*.
I have learned that it is much faster to write be means of FILE then by means of ofstream.
But when char* is bigger, "FILE" method produce corupted file.
Here is an example:

void write_file()
{
// SIZE TO BE WRITTEN
long lSizeOfArr = 40000000;

//prepare char buffer
char *buf;
buf = (char *)malloc(lSizeOfArr);

// fill buffer
for(long l = 0; l < lSizeOfArr; l++ )
{
buf[l] = 'A';
}

// write by means of ofstream
ofstream MyFile;
MyFile.open(".//file1.dat",ios::binary );
MyFile.write(buf,lSizeOfArr);
MyFile.close();

// write by means of FILE
FILE *dst;
dst = fopen(".//file2.dat", "wb");
long lWritten = fwrite(buf, 1, lSizeOfArr, dst);
fclose(dst);

// free buffer
free(buf);
}

When I set lSizeOfArr = 10 milions, both method work fine. But when I inrease lSizeOfArr to 40 milions, 1st method works fine, second method produces corrupted file.
File "file2.dat" has size 39 997 440 bytes. Return value of fwrite - lWritten is set to 0.
Do you know wheather there is any limit for fwrite size or where my code has a bug?

Yves M
February 3rd, 2004, 01:52 PM
That's very strange. 40 million records is far less than any problematic spot like 2GB files which can't exist on FAT32 e.g.

By the way, writing 400 million bytes using the exact same code you posted above correctly works on my computer. I'm sure you have enough free space on your drive, no?

What OS, compiler and filesystem are you using?

spring
February 3rd, 2004, 03:21 PM
I tested this on my second PC and it works correctly. It's seems that my first PC has corrupted HD. MS Windows 98 complaining during reboot and wants to run scandisc and surface test has to be performed.
So, error is in my HD, but for me is strange, that writting by means of ofstream works OK, by means of FILE is more sensittive.

Yves M
February 3rd, 2004, 04:27 PM
The difference is probably due to chance and the local weather conditions. Had the ofstream tried to write to the same location as fwrite, it would have failed too.