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

This is a discussion on Can you have an ifstream and ofstream open for one file at once? within the C++ Programming forums, part of the General Programming Boards category; This is my first post, so please go easy on me . I intend to both read from and write ...

  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 12:55 PM.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    fstream file; //fstream object
    file.open("myfile.txt", ios::in|ios:: out); //open for input and output
    Hope that helps.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Great, that helps a lot. My only question then is how do I ensure that I am not opening a pre-existing file for output; in other words, I wish to prevent any sort of overwriting. Thanks again!

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    fstream file; //fstream object
    file.open("myfile.txt", ios::in|ios:: out|ios:: app); /*open for input and output Appending to file only*/
    I believe that's it.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Sorry, what I meant was basically "do not open the file if it already exists." I don't want to be able to write to a file if it already exists.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    317
    Well try replacing ios:: app with ios:: nocreate

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    317
    Allright, forget that, I can't read today, probably need to use an if statement like so:

    Code:
    if(!(file.open("myfile.txt", ios:: in|ios:: out|ios::nocreate))){
                // do reading and writing
    }
    else{
                 cout<<"File aready exists";
    }

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    wouldnt it be more like this:

    Code:
    if(file.open("file.txt"), ios::in  | ios::out | ios::nocreate) 
    {
         cout << "File already exists";
    } else {
         // do reading and writing
    }
    [EDIT]Stupid Smilies[/EDIT]
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    317
    If you look again you'll see the '!' operator(not), essentially both are codes evaluate the same. I hate those smilies too.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    oooo sorry didnt see the ! there

    [EDIT]Sureee smilies dont apear when i want them to[/EDIT]
    "There are three kinds of people in the world...
    Those that can count and those that can't."

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, 03: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, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21