Thread: Writing to begging of file with fstream

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Writing to begging of file with fstream

    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));
    // Seek to the end of the first integar entry
    out2.seekp(sizeof(int), ios::beg);
    // Change the existing value to "7"
    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
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You open the file with ios::app (append mode). In that mode you cannot seek for a write effectively since every write first moves the file pointer to the end of the file. Get rid of ios::app, add ios::in (as well as ios:: out), and seek to the end to append.
    Code:
      fstream out2("file.dat", ios::in | ios::out | ios::binary);
      out2.seekp(0, ios::end);
      out2.write((char*)&d, sizeof(int));
      out2.write((char*)&e, sizeof(int));
      out2.write((char*)&f, sizeof(int)); // Seek to the end of the first integar entry
      out2.seekp(sizeof(int), ios::beg);  // Change the existing value to "7"
      out2.write((char*)&g, sizeof(int));
      out2.close();
    And you should probably only print 6 numbers in your for loop, since that's all you're writing

    to the file. Or better yet, use more general loop control:
    Code:
      while (in.read((char*)&x, sizeof(int)))
        cout << x;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm begging for help: How to use SAPI in C?
    By premudriy in forum C Programming
    Replies: 6
    Last Post: 10-07-2010, 01:28 PM
  2. Please Help A Noob Here (plz Im Begging You)
    By bobbie18 in forum C Programming
    Replies: 97
    Last Post: 03-28-2008, 03:38 AM
  3. Help me to this please I'm begging..
    By lesrhac03 in forum C Programming
    Replies: 23
    Last Post: 03-27-2008, 05:43 PM
  4. FILE* from fstream
    By Mortissus in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2005, 07:07 AM
  5. Begging on wall street
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-04-2004, 04:50 PM