In C++, we can interact with files. We always #include <fstream>
, and:
- Open files with:
ifstream inFile("myfile")
, as in “input file stream” with nameinFile
.- And we should ideally
close()
the file before weopen()
another one. - By default, this overwrites all existing content on the file, otherwise a new file is created.
- Otherwise, we can open to append content, i.e., what we write adds to the end of the existing file. Use:
outFile.open("foo.txt", ios::app)
- And we should ideally
- Read from a file:
- Output to a file:
Methods
- And closing files:
inFile.close()
- For file output, we can:
- When we reach the end of the file, we use:
inFile.eof()
, which evaluates to true if we reached the end. fail()
returns true if there’s any error, checks if there’s a flaginFile
is an object, has an error flag- You can use this approach on
inFile
,cin
,cout
,outFile
or any other stream
Buffering
Output is not immediately written to a file, instead temporarily stored in a buffer, then written out to the file periodically in chunks. What this means is that our results don’t show up immediately. Why does this happen? Writing to the disk is pretty slow.
We sometimes flush out the buffer. With respect to file outputs, we can use outFile.flush()
or outFile << endl
.
Input
Inputs are also buffered. No access to input data until user uses the ENTER
key. Data is extracted character-by-character or byte-by-byte. Strings are read until a whitespace. For other types, it reads char-by-char until a conversion isn’t possible. See below.
Whitespaces are skipped, i.e.,
, \t
, \n
, etc.
Things that could go wrong
Some things can go wrong:
- The file doesn’t exist or cannot open
- Variables of a certain type cannot be read (i.e., we get some other type)
So we have to detect errors and be able to handle them:
Alternatively: