ofstream writing replaces entire file with 0s
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: ofstream writing replaces entire file with 0s

  1. #1
    Join Date
    Jun 2012
    Location
    .NET 4.0 / VS 2010
    Posts
    20

    ofstream writing replaces entire file with 0s

    I have a std::vector of short ints that I need to write to a specific location in a binary file (without using .NET code). To that end, I wrote up this code:
    Code:
        ofstream fileStream (filePathString, ios::out | ios::binary);
        int curPos = 2821;
        fileStream.seekp(curPos);
        int iter = 0;
        while (iter < 1024*1024){
            char bytesToWrite[2];
            //Low byte
            bytesToWrite[0] = LOBYTE(dataVector[iter]);
            //High byte
            bytesToWrite[1] = HIBYTE(dataVector[iter]);
            fileStream.write(&bytesToWrite[0], 2);
            curPos += 2;
            fileStream.seekp(curPos);
            iter++;
            delete[] bytesToWrite;
        }
        fileStream.close();
    The code runs without crashing, but when I look at the file afterwards in a hex editor, every byte (even those outside the range I thought I was writing to) are replaced with 00. I suspect I'm missing something in my understanding of file streams. Did I write that code correctly? Seekp does move the pointer over the next byte to be overwritten, yes? Am I getting a memory leak somewhere?

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,625

    Re: ofstream writing replaces entire file with 0s

    1) you should not "delete [] bytesToWrite" ... byteToWrite was not created using new []

    2) You have:

    Code:
    ofstream fileStream (filePathString, ios::out | ios::binary);
    int curPos = 2821;
    fileStream.seekp(curPos);
    I do not know what the seekp will do, since you create an empty file
    and try to seek to position 2821 (which does not exist)

    3) I assume that the file already exists, so you can try openning with:
    Code:
    ofstream fileStream (filePathString, ios::out | ios::in | ios::binary);
    4) I think the entire loop could be replace with:

    Code:
    fileStream.write(reinterpret_cast<const char *>(&dataVector[0]),1024*1024*sizeof(short));

  3. #3
    Join Date
    Jun 2012
    Location
    .NET 4.0 / VS 2010
    Posts
    20

    Re: ofstream writing replaces entire file with 0s

    Huh. That single line replacing the entire loop worked perfectly. Thanks!

    But what do you mean I was creating an empty file? You're right, filePathString is the path to a file that already exists. I thought this:
    Code:
    ofstream fileStream (filePathString, ios::out | ios::binary);
    would open that file, and then seekp would work as expected? What's the difference between what I have and what you suggested in #3?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: ofstream writing replaces entire file with 0s

    Quote Originally Posted by fiodis View Post
    The code runs without crashing,
    Code:
            char bytesToWrite[2];
    //...
            delete[] bytesToWrite;
    This does not "run fine". You are calling delete[] on a pointer that was never initialized with new[]. Once that illegal line is executed, the behaviour of the program becomes undefined.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 3rd, 2013 at 05:04 PM.

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,110

    Re: ofstream writing replaces entire file with 0s

    ios:ut erases an existing file. So if you open an existing file with that option, then seekp to a position, all bytes before that position are filled with 0x0.
    It's not Philip Nicoletti's suggestion #4 that did the trick, it's #3 which opens an existing file for read/write access. IIRC, using this option on a non-existing file, then trying to write to it would fail. Watch out for the pitfalls of the filestream:pen options...

    HTH,
    Richard
    Last edited by Richard.J; January 3rd, 2013 at 05:22 PM. Reason: sorry, could not find the option to disable those smilies

  6. #6
    Join Date
    Jun 2012
    Location
    .NET 4.0 / VS 2010
    Posts
    20

    Re: ofstream writing replaces entire file with 0s

    I see. Thanks everyone!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center