cannot open file


I'm trying to open a file with fstream but is_open always returns false. What are the possible reasons for the file not being created?

1
2
3
4
5
6
7
8
fstream myfile;

    myfile.open ("students.txt", fstream::in | fstream::out );

    if (!myfile.is_open())
    {
        cout<<"Error";
    }

students.txt might not be in the same working directory as your application.
IIRC open( in|out ); does not create the file.
you need to open( out );
Ok Thank you, now is working, but to insert data in the file, just open (out); going to work?
closed account (3qX21hU5)
Yes fstream::in means that you want to read from the file, where as fstream::out means you want to write to the file.

Just remember if you open the file associated to a fstream with out mode, but not in mode specified the file is truncated.
Last edited on
Ok the fact of being truncated, does not matter, because do not want to save information.
What I want, is to create a file that does not exist, type text to read (because I need to do data validation) and enter information.

So what do I need to put in order to perform these actions?
Topic archived. No new replies allowed.