Thread: Can you have an ifstream and ofstream open for one file at once?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    13

    Can you have an ifstream and ofstream open for one file at once + no overwriting?

    This is my first post, so please go easy on me .

    I intend to both read from and write to a file. Can I have an ifstream and ofstream open for the same file at the same time? Or do I have to close one before I open the other?

    Also, will the following code work to ensure the file I am writing to does not already exist, thus preventing overwriting?

    Code:
        ofstream target_out; // ofstream to write to target file
        bool done = false;
        char targetname[80];
    
        while( !done ) {
            cout << "Enter data target file: ";
            cin >> targetname;
            target_out.open( targetname, ios::nocreate );
    
            if( target_out.is_open() ) {
                cout << "The file you entered already exists." << endl;
                target_out.close();
            }
            else
                done = true;
    
        }
    
        target_out.open( targetname, ios::noreplace );
    Thanks!
    Last edited by mosdef; 06-06-2002 at 11:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Values not going to external file
    By kristentx in forum C++ Programming
    Replies: 9
    Last Post: 09-26-2007, 02:51 PM
  2. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 11:40 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 08:39 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM