Files for Input and Output (sec 3-7)
Seefileio.cpp
File Stream fstream
If you want to save the output of a program for later use, you may instruct the program to write the output to a file.
You might not want to re-enter the data every time you run the program. You may store the data in a file and instruct the program to read the data from the file. To prepare the file, open a new file in your editor. Enter the data and save the file in the same directory as your program.
To use files in C++, you must add another #include statement at the beginning of the program. While #include <iostream> permits use of the functions that work with iostreams like cin, cout, and cerr, it is necessary to add #include <fstream>to allow use of the functions that work with files.
ifstreamand ofstream
To use a file in C++, you must declare and open the file.
For input, declaring the file is done usingifstream; for output, usingofstream.
ifstream infile;
ofstream outfile;
The line below associates the internal name infilewith the actual file on disk called "p3.dat", and opens that file.
infile.open("p3.dat");
The line below associates the internal nameoutfile with the file on disk called "p3.out", and opens that file:
outfile.open("p3.out");
The internal name for a file may be anything you wish, but it is customary to use names that reflect the use of the file (infile, outfile, data, etc.).
SKIP: When specifying the external name, you should give the full name by which the file is known in the system (for example, if file "p3.dat" is on your a: disk, you must say "a:p3.dat"). [Note to me: Not sure you don’t need one or two slashes in a:p3.dat]
[Note to me: opened the file as you declared the stream by passing the filename as a parameter to the constructor of the object. ofstream fout("output.txt");]
The above style is particularly useful when you want to open a file more than once in a program, or when you want to use a file in several functions in a program (see Chapter 5). Declaring the fileabove main makes the file available for use in the main program as well as in other functions the program uses (see Chapter 5). The file must be opened before the first use of the file. Do not open a file more than once (unless you close it in between), because each call toopen() starts reading or writing over again at the beginning of the file.
The statements above do two things: they declare the internal name (infile or outfile), and they associate the internal name with the external name ("p3.dat" or "p3.out").
You’ve noticed that these statements are commented out in the examples. When the file will be used in onlyone function (See chap. 5), declaring the file can be done in combination with opening the file (like initializing a variable when you declare it). This statement declares the input file named infile and opens it:
ifstream infile("p3.dat");
The same for output:
ofstream outfile("p3.out");
Using a File
Once a file has been opened, you can use it by substituting its internal name where you would have used cin or cout. The following statement writes the value of num to the file known internally as outfile.
outfile < num;
Closing Files
When you are finished using a file, you should close it, as shown below. The system will generally close the file if you don't explicitly close it, but failure to close a file can cause problems, and you should get into the habit of explicitly closing all files.
infile.close();
outfile.close();
SEE Harrow’s Example.
See fileio.cpp
Read sections 8 and 9 for your own interest. I won’t test.