Start New Discussion within our Software Development Community

When I'm opening a file, is it the file that is text or binary or is it the stream that can be text or binary?

Also, when I close a file is that when the stream object is destroyed?

>>When I'm opening a file, is it the file that is text or binary or is it the stream that can be text or binary?

Both. You can open any file in either text or binary mode. When you open a binary file in text mode your program will probably just read a whole lot of junk because the stream will mis-interpret the bytes in the file.

If you have a file that was written with the stream opened in binary file, try to load the file in a text editor such as Notepad. What you will see is a lot of unreadable characters, a lot of spaces, and/or a bunch of graphic squares.

>>when I close a file is that when the stream object is destroyed?
No. Like any other object that was created on the stack it is destroyed when the function returns

void foo()
{
   fstream fin; // create an object

   ...
   ...
   // last line of the function will destroy the fstream object
}
This question has already been answered. Start a new discussion instead.