CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2006
    Posts
    98

    fstream problems

    Hello,

    I am trying to stream data to a file, and then return to the file to add further data. When I add data the second time, I then want to update the value of the second byte in the whole file. I can't seem to do this!
    Here is my sample code:

    Code:
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;
    int g = 7;
    int x;
    
    fstream out1("file.dat", ios::out | ios::binary | ios::trunc);
    out1.write((char*)&a, sizeof(int));
    out1.write((char*)&b, sizeof(int));
    out1.write((char*)&c, sizeof(int));
    out1.close();
    
    fstream out2("file.dat", ios::out | ios::binary | ios::app);
    out2.write((char*)&d, sizeof(int));
    out2.write((char*)&e, sizeof(int));
    out2.write((char*)&f, sizeof(int));
    out2.seekp(sizeof(int), ios::beg);
    out2.write((char*)&g, sizeof(int));
    out2.close();
    
    fstream in("file.dat", ios::in | ios::binary);
    in.seekg(0, ios::beg);
    for (int i = 0; i < 10; i++)
    {
    	in.read((char*)&x, sizeof(int));
    	cout << x;
    }
    The output I get is "1, 2, 3, 4, 5, 6", but I want to be getting "1, 7, 3, 4, 5, 6", because in "out2", I seekp to the second integar entry, and change it to "7".

    I have also tried using ios::ate in the constructor for "out2", but this gives me the out put "4, 7, 6, 6, 6, 6", which is suggesting that when I create my fstream object "in", any seekg commands are relative to the beginning of the "out2" stream, rather than the "out1" stream.

    Thanks for any help!

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: fstream problems

    http://www.cplusplus.com/reference/i...tream/fstream/

    "app (append) : Set the stream's position indicator to the end of the stream before each output operation."

    You want ate:
    "ate (at end) : Set the stream's position indicator to the end of the stream on opening"
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Aug 2006
    Posts
    98

    Re: fstream problems

    Hi,

    Thanks for your help. However, I have tried using ios::ate instead of ios::app, and the output I got was "4, 7, 6, 6, 6, 6". So it seems that when I seekp, it is seeking relative to the out2 stream, rather than the out1 stream. If it had seeked to the beginning of the out2 stream, the output would be "1, 7, 3, 4, 5, 6".

    It seems that you can only seek as far back as the start of LAST output stream - but I want to seek back to the start of the FIRST output. What's going on here?

    Thanks!

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: fstream problems

    1) ios::out | ios::ate will empty the file (so the second set of writes start at the beginning.

    2) You can use:
    Code:
    ofstream out2("file.dat", ios::out | ios::in | ios::binary | ios::ate);
    3) Also, you are trying to write out more data that is in the file. You can use something like:

    Code:
    while (in.read((char*)&x, sizeof(int)))
    {
      cout << x;
    }

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