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?
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?