Thread: Help with output to files

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    Help with output to files

    I am trying to make a simple phone book. I've got everything all right right down to the actual output to the file.

    Code:
        cout << "Is this information correct? (Y or N)";
                     ch = getch();
                     
                     if(ch == 'y' || ch == 'Y'){
                          ofstream file;
                          file.open("Book.txt", ios::ate);
                          file << data.fname << " " <<data.lname << endl
                               << data.phnum << endl
                               << data.addy << endl
                               << data.cmt << endl << endl;
                          file.close();
                          menu();
                 }
    I used ios::ate to open the file and go to the end of the file, but when I try to add a second entry, it overwrites the first.

    Any help at all would be appreciated.

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You want "ios::ate | ios::out".

    gg

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    6
    It is still overwriting each time a new entry is added.

    Code:
        cout << "Is this information correct? (Y or N)";
                     ch = getch();
                     
                     if(ch == 'y' || ch == 'Y'){
                          ofstream file;
                          file.open("Book.txt", ios::ate | ios::out);
                          file << data.fname << " " <<data.lname << endl
                               << data.phnum << endl
                               << data.addy << endl
                               << data.cmt << endl << endl;
                          file.close();
                          menu();
                 }
    EDIT:

    I see now.. I need to also add ios::app

    Thanks CP
    Last edited by UnderGod; 05-11-2004 at 11:40 AM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    My bad.
    According to the C++ standard, ios::out is the equivalent of fopen()'s "w", that is a new file is created or an old one is truncated.
    So in other words, "ios::out" and "ios::out | ios::trunc" will result in identical behavour.

    Here's the table right out of the C++ standard:
    Code:
    B - ios::binary
    I - ios::in
    O - ios::out
    T - ios::trunc
    A - ios::app
    Eq - fopen() equivalent
    
    B I O T A Eq
    --------------
        +     "w" 
        +   + "a" 
        + +   "w" 
      +       "r" 
      + +     "r+" 
      + + +   "w+" 
    +   +     "wb" 
    +   +   + "ab" 
    +   + +   "wb" 
    + +       "rb" 
    + + +     "r+b" 
    + + + +   "w+b"
    These are all the valid combinations and any combination not in the table should cause open to fail (this is rarely the case with most implementations).

    Notice that ios::ate isn't even in the table. It's really just a flag to seek to the end of the file once the file has been opened.

    If you really want to use ios::ate instead of ios::app, you have to open the file for output in a way that does not truncate the file - that is "ios::in | ios::out | ios::ate".

    The difference between that and "ios::out | ios::app" is that ios::app causes all output to be appended to the file, regarless of an any seekp() calls.

    gg

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    6
    Thats no problem though.

    Now I just need to figure out on my own a "search" feature which I am thinking would work with using getline() and setting every line to a variable, and then use str functions to read whats inside the strings.

    Thanks again though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Input and Output files
    By matrixhelp in forum C Programming
    Replies: 1
    Last Post: 03-10-2008, 02:07 AM
  3. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Need Help With Naming Output Files!!!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 12:52 PM