C++ fstream, ifstream, ofstream
C++ fstream, ifstream, ofstream
ifstream
- constructor
- default constructor
- Constructs an ifstream object that is not associated with any file.
- Internally, its istream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer).
- initialization constructor
- Constructs an ifstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
- Internally, its istream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). Then, filebuf::open is called with filename and mode as arguments.
- If the file cannot be opened, the stream’s failbit flag is set.
- copy constructor (deleted)
- Deleted (no copy constructor).
- move constructor
- filename
- A string representing the name of the file to open.
- Specifics about its format and validity depend on the library implementation and running environment.
- mode
- Flags describing the requested i/o mode for the file.
member constant | stands for | access |
---|---|---|
in * | input | File open for reading: the internal stream buffer supports input operations. |
out | output | File open for writing: the internal stream buffer supports output operations. |
binary | binary | Operations are performed in binary mode rather than text. |
ate | at end | The output position starts at the end of the file. |
app | append | All output operations happen at the end of the file, appending to its existing contents. |
trunc | truncate | Any contents that existed in the file before it is open are discarded. |
open
Opens the file identified by argument filename, associating its content with the file stream buffer object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode.
If the object is already associated with a file (i.e., it is already open), this function fails.
parameter
filename
mode
return
- The function returns this if successful.
- In case of failure, the file is not open, and a null pointer is returned.
reference
- filebuf::open - C++ Reference
is_open
- Check if a file is open
- Returns whether the stream is currently associated to a file.
- Streams can be associated to files by a successful call to member open or directly on construction, and disassociated by calling close or on destruction.
reference
good, fail, bad
- failbit is generally set by an operation when the error is related to the internal logic of the operation itself; further operations on the stream may be possible. While badbit is generally set when the error involves the loss of integrity of the stream, which is likely to persist even if a different operation is attempted on the stream.
- badbit can be checked independently by calling member function bad:
- Notice that this function is not the exact opposite of good, which checks whether none of the error flags (eofbit, failbit and badbit) are set, and not only badbit:
iostate value | indicates | good() | eof() | fail() | bad() | rdstate() |
---|---|---|---|---|---|---|
goodbit | No errors (zero value iostate) | true | false | false | false | goodbit |
eofbit | End-of-File reached on input operation | false | true | false | false | eofbit |
failbit | Logical error on i/o operation | false | false | true | false | failbit |
badbit | Read/writing error on i/o operation | false | false | true | true | badbit |