Files and Streams
Why Use Files?
All the programs we have looked at so far use input only from the keyboard, and output only to the screen. If we were restricted to use only the keyboard and screen as input and output devices, it would be difficult to handle large amounts of input data, and output data would always be lost as soon as we turned the computer off. To avoid these problems, we can store data in some secondary storage device, usually magnetic tapes or discs. Data can be created by one program, stored on these devices, and then accessed or modified by other programs when necessary. To achieve this, the data is packaged up on the storage devices as data structures called files.
The easiest way to think about a file is as a linear sequence of characters.
Streams
Before we can work with files in C++, we need to become acquainted with the notion of a stream. We can think of a stream as a channel or conduit on which data is passed from senders to receivers. As far as the programs we will use are concerned, streams allow travel in only one direction. Data can be sent out from the program on an output stream, or received into the program on an input stream. For example, at the start of a program, the standard input stream "cin" is connected to the keyboard and the standard output stream "cout" is connected to the screen.
In fact, input and output streams such as "cin" and "cout" are examples of (stream) objects. So learning about streams is a good way to introduce some of the syntax and ideas behind the object-oriented part of C++. The header file which lists the operations on streams both to and from files is called "fstream". We will therefore assume that the program fragments discussed below are embedded in programs containing the "include" statement
#include<fstream>
As we shall see, the essential characteristic of stream processing is that data elements must be sent to or received from a stream one at a time, i.e. in serial fashion.
Creating Streams
Before we can use an input or output stream in a program, we must "create" it. Statements to create streams look like variable declarations, and are usually placed at the top of programs or function implementations along with the variable declarations. So for example the statements
ifstream in_stream;
ofstream out_stream;
respectively create a stream called "in_stream" belonging to the class (like type) "ifstream" (input-file-stream), and a stream called "out_stream" belonging to the class "ofstream" (output-file-stream). However, the analogy between streams and ordinary variables (of type "int", "char", etc.) can't be taken too far. We cannot, for example, use simple assignment statements with streams (e.g. we can't just write "in_stream1 = in_stream2").
Connecting and Disconnecting Streams to Files
Having created a stream, we can connect it to a file using the member function "open(...)". (We have already come across some member functions for output streams, such as "precision(...)" and "width(...)", in Lecture 2.) The function "open(...)" has a different effect for ifstreams than for ofstreams (i.e. the function is polymorphic).
To connect the ifstream "in_stream" to the file "Lecture_4", we use the following statement:
in_stream.open("Lecture_4");
This connects "in_stream" to the beginning of "Lecture_4". Diagramatically, we end up in the following situation:
Figure 4.2.1
To connect the ofstream "out_stream" to the file "Lecture_4", we use an analogous statement:
out_stream.open("Lecture_4");
Although this connects "out_stream" to "Lecture_4", it also deletes the previous contents of the file, ready for new input. Diagramatically, we end up as follows:
Figure 4.2.2
To disconnect connect the ifstream "in_stream" to whatever file it is connected to, we write:
in_stream.close();
Diagramatically, the situation changes from that of Figure 4.2.1 to:
Figure 4.2.3
The statement:
out_stream.close();
has a similar effect, but in addition the system will "clean up" by adding an "end-of-file" marker at the end of the file. Thus, if no data has been output to "Lecture_4" since "out_stream" was connected to it, we change from the situation in Figure 4.2.2 to:
Figure 4.2.4
In this case, the file "Lecture_4" still exists, but is empty.
(BACK TO COURSE CONTENTS)
4.3 Checking for Failure with File Commands
File operations, such as opening and closing files, are a notorious source of errors. Robust commercial programs should always include some check to make sure that file operations have completed successfully, and error handling routines in case they haven't. A simple checking mechanism is provided by the member function "fail()". The function call
in_stream.fail();
returns True if the previous stream operation on "in_stream" was not successful (perhaps we tried to open a file which didn't exist). If a failure has occurred, "in_stream" may be in a corrupted state, and it is best not to attempt any more operations with it. The following example program fragment plays very safe by quitting the program entirely, using the "exit(1)" command from the library "cstdlib":
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream in_stream;
in_stream.open("Lecture_4");
if (in_stream.fail())
{
cout << "Sorry, the file couldn't be opened!\n";
exit(1);
}
...
|