5.9.3.4. Iostream: Reading from a File (optional)

To use the things described here, #include the <fstream> library.

ifstream in(path);

ifstream is a type. This statement creates an ifstream object called in that reads from the file described by string path.

See in.fail() below.


in >> x;

Do with an open ffstream object all of the same things that you can do with cin.

in.fail()

Expression in.fail() is true if the most recent attempt to read from file in could not be done, or if file in could not be opened. You should be sure to test this after opening a file, since the file that you are trying to open might not exist or might not be readable. For example, write
  ifstream in("data.txt");
  if(in.fail())
  {
    (what to do if the file could not be opened)
  }

in.close()

Use this to close file in when you are done reading from it.