c++ help with ofstream

Discussion in 'OT Technology' started by Glennhuricane, Dec 7, 2005.

  1. Glennhuricane

    Glennhuricane New Member

    Joined:
    Apr 2, 2004
    Messages:
    6,070
    Likes Received:
    0
    Location:
    nj
    my problem is i want "name address and phone #" to always be in the file
    and i want the actual data to append to the file not overwrite it
    I am not sure of the snytax. here is the code, if somebody could help that would be great


    void Directory::savetofile()
    {
    ofstream outPhonebookFile("phonebook.txt");
    outPhonebookFile << setw(20) << "***NAME***" << setw(20) << "***ADDRESS***"\
    << setw(20) << "***PHONE #***" << endl;
    outPhonebookFile ios::app ; <------------------this is the line that i don't know
    for (int i = 0; i < currentSize; i++)
    outPhonebookFile << entryList << endl;
    exit(1);
    }
     
    Last edited: Dec 7, 2005
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    you need to use the std::ios::app flag in the ofstream ctor

    Code:
    #include <fstream>
    
    int main() {
            std::ofstream foo("foo.txt", std::ios::app);
            foo << "hello world";
            return 0;
    }
    
     
  3. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,308
    Likes Received:
    563
    Change this:
    ofstream outPhonebookFile("phonebook.txt");

    to this:
    ofstream outPhonebookFile("phonebook.txt",ios::out | ios::app);

    and delete this:
    outPhonebookFile ios::app ;

    and it should work.
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,308
    Likes Received:
    563
  5. alex

    alex My neighbor got the gas chamber awhile back for do Moderator

    Joined:
    Oct 11, 2005
    Messages:
    24,383
    Likes Received:
    8
    Location:
    Orange County, NY
    :werd:

    bool cplusplus_ftw = TRUE; :cool:
     

Share This Page