Reputation: 4089
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?
Upvotes: 0
Views: 2041
Reputation: 153955
The std::ifstream
is set up to own a std::filebuf
which is a class derived from std::streambuf
. The stream buffer is managing buffering for streams in a generic way and abstracts the details of how a stream is accessed. For a std::filebuf
the underlying stream is an operating system file accessed as needed. When std::ifstream::open()
is called this call is mainly delegated to std::filebuf::open()
which does the actual work. However, the std::ifstream
will clear()
its state bits if the call to std::filebuf::open()
succeeds and set std::ios_base::failbit
if the call fails. The file buffer will call the system's method to allocate a file handle and, if successful, arrange for this file handle to be released in its destructor or in the std::filebuf::close()
function - whatever comes first. When calling std::ifstream::open()
with the default arguments the system call will check that the file exists, is accessible, not too many file handles are open, etc. There is an std::ios_base::openmode
parameter which can be used to modify the behavior in some ways and when different flags are used when calling std::ofstream::open()
.
Whether the call to std::filebuf::open()
has any other effects is up to the implementation. For example, the implementation could choose to obtain a sequence of bytes and convert them into characters. Since the user can override certain setting, in particular the std::locale
(see the std::streambuf::pubimbue()
function), it is unlikely that much will happen prior to the first read, though. In any case, none of the state flags would be affected by the outcome of any operation after opening the file itself.
BTW, the mentioned classes are actually all templates (std::basic_ifstream
, std::basic_filebuf
, std::basic_streambuf
, and std::basic_ofstream
) which are typedef
'ed to the names used above for the instantiation working on char
as a character type. There are similar typedef
s using a w
prefix for instantiations working on wchar_t
. Interestingly, there are no typedef
s for the char16_t
and char32_t
versions and it seems it will be a bit of work to get them instantiated as well.
Upvotes: 2
Reputation: 66951
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.
Upvotes: 2
Reputation: 3111
If you think logically, ifstream is just the stream in which we will get our file contents. The parameters, we provide to ifstream.open() will open the file and mark it as open. When the file is marked as open, it will not allow you to do some operations on file like renaming a file as it is opened by some program. It will allow you to do the same after you close the stream. ifstream - imo is only the helper class to access files.
Upvotes: 1
Reputation: 68698
It's not clear if you want to know implementation details or standard requirements - but as for implementation details - it will call the underlying open system call on the operating system. On Linux for example this is called open
. On Windows it is called CreateFile
.
Upvotes: 3