Thread: fstream confusion

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    fstream confusion

    My objective is to write to a file and read it back. All the reading is done after all the writing.
    Code:
    // first version
    ofstream outFile("file.txt", ios::binary | ios::out | ios::in);
    ifstream inFile("file.txt", ios::binary | ios::in);
    With codes above, file.txt is not created when I run it.

    Code:
    // second version
    ofstream outFile("studentVer1.txt", ios::binary | ios::out);
    ifstream inFile("studentVer1.txt", ios::binary | ios::in);
    With these ones, file.txt is created but reading is unsuccessful.

    When I do first version and create a blank file.txt, first run will write but fail to read, second run and after will write and read.

    So, whatsupp??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can't have two separate handles to a file which read and write to that file.

    If you want read and write access, then you have a single handle, and you use ios:ut | ios::in to open it, and seek() to move to where you want to read and write
    And don't forget flush() after writing
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I did this
    Code:
    fstream file("file.txt", ios::binary | ios::out | ios::in);
    and after each writing I followed by
    Code:
    file.flush();
    and right before I started reading
    Code:
    file.seekg(0);
    But...still no file.txt is created, and if I create blank file.txt, it will write and read fine. Any suspicion?
    Anybody has the same case before? I'm afraid that some other codes in my program that cause it, but it's a lot of codes to post. So, any suggestion what to check?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't know about ios::binary, but using ios:ut|ios::in will open a file only if it already exists...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To create a file if it doesn't exist, not overwrite the data in the file if it does exist, and start at the beginning of the file then you can try this:

    ofstream fout(filename, ios::in | ios:ut | ios::trunc);

    I'm not sure it does all that, but I think it does, and it won't hurt to try on a sample file. Alternatively look for existense first, create the file second if need be.
    Code:
    ofstrean fout;
    ifstream fin(filename);
    if(!fin.is_open())
    {
      fout.open(filename);
    }
    else
    {
      fin.close();
      fout.open(filename, ios::in | ios::out);
    }
    Using an fstream may be a little easier.

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Adding ios::trunc WORKS. Thanx a bunch!
    EDIT: but I thought ios::trunc is the default action for ios:ut?
    So, if you use ios:ut | ios::in at the same time, ios::trunc is not a default action anymore?
    Last edited by alphaoide; 10-06-2003 at 09:50 AM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yup, weird, but that's the way I understand it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  2. using fstream
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 04:53 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM