Search code examples
c++file-iofilestreamfstream

What does ifstream::open() really do?


Consider this code:

ifstream filein;

filein.open("y.txt");

When I use the open() function, what happens?

  • Does the file stream itself get opened?

  • or does the object's state change to open?

  • or both?


Solution

  • The filestream being open or closed is represented by it's state. So if you change the state to open, the filestream is now open. Like a doorway. If you open it, you've changed it's state to the open position. Then you can later close it, which involves changing it's state to the closed position. Changing its state to open and opening the stream are the exact same thing.