CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2004
    Location
    Czech Republic, Brno
    Posts
    20

    Limits for fwrite

    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?

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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?
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Jan 2004
    Location
    Czech Republic, Brno
    Posts
    20

    Hard disc corrupted

    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.

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured