ifstream and Flat Files

The ifstream object is used to input from files. For now, you should think of an object or a class as a group of related functions all grouped together into a single bundle. That bundle is what we are calling a class or an object. We will be exploring in greater detail exactly what classes and objects are later in this book. In fact, three chapters are devoted to this topic.

The word stream in the name should be of interest to you. Files are input and output in C++ in the form of a stream. A stream is simply a running thread in memory. It is literally a stream of bytes, with each byte representing a character in the file. In the C++ programming language, all input and output occurs in streams of bytes. A stream is simply a sequence of bytes. Input streams receive bytes from some device—usually the keyboard or, in the case of ifstream, a file on the disk.

Basically, the ifstream class is a group of functions used to input files. The ofstream class is used to output to files. The functionality of both are combined in fstream. These objects can be used in your code to represent specific files, and to manipulate those files. You can input and output to a file using ifstream, ofstream, and fstream. Each of these objects will treat the file as a stream of bytes, with each byte representing a single character in the file. Thus, this is the reason for the word stream in the names of these objects. It is the same as with cout and cin from the iostream header file. They treat the keyboard input and the screen output as a stream of bytes. The only real difference is that cout sends data to the screen and ofstream sends it to a flat file. They both handle the data in much the same manner.

Output streams send their sequence of bytes to some device—usually the monitor—but in the case of ofstream the bytes are sent to some file on the hard disk. As was mentioned earlier, the underlying function is the same as output you saw in Chapter 2, it is only the destination that is really different.

You can create an instance of this class just like you would any other variable with one exception. After the name you give your variable you will have open and close parentheses. In some cases, these parentheses will have parameters in them; in others they will not. The following is the most common, and the simplest, way to create an instance of this ifstream.

ifstream fin(); 

With this line of code, we have just created a variable called fin. That variable is of the type ifstream.That means that it is an object that represents a file to be input. This object, ifstream, has a number of methods and properties that you will be able to use in your programs. Table 6.1 summarizes the most commonly used of these.

Table 6.1: ifstream Methods and Properties




This method simply clears the contents of the buffer.


Closes the file you previously opened.


This property is a Boolean and tells you if you are at the end of the file you have opened.


This method gets a single character.


This method gets an entire line.


This is a Boolean property that is true if the file is open and false if it is closed.


Opens a file and attaches it to the filebuf object and, thus, to the stream.


Sets the stream’s mode to binary or text.


This method simply reads in data from the stream. The data will be in binary mode.


When a file is opened in binary mode you can tell it to go to a particular space in the file. This method does that.


This method retrieves the current position of the pointer in the file.

You can see that a lot of these methods mention a stream. You might be wondering what this is referring to. The most important thing for you to remember is that C++ handles file input and output as streams of characters. Actually, it handles screen/keyboard input and output the same way, but with cout and cin it’s not as obvious. A stream is simply a flow of individual characters, flowing much like a stream of water, thus the name.

The properties are designed simply to let you know the current state of the file. (Is it open or not?) The various methods you see in Table 6.1 are primarily concerned with opening the file, reading it in (either one character at a time, or one line at a time), and closing the file. The exception is the setmode method. It determines how the file will be opened. By default it is opened as text. You do, however, have some options as to how you open it. You can set the mode to text or binary. You will probably use the text mode most of the time.

Example 6.1

This example will illustrate how to read-in text data from a text file. The data is simply read in to a variable then displayed on the screen.

Step 1: Enter the following code into your favorite text editor.

#include <iostream>    #include <fstream> using namespace std; int main ()     {  char buffer[256];  ifstream myfile ("test.txt");  while (! myfile.eof() )  {    myfile.getline (buffer,100);    cout << buffer << endl;  }  return 0; }

Step 2: Compile the code.

Step 3: Run the code. You should see something like what is shown in Figure 6.1C.

click to expand
Figure 6.1C: File input.

Let’s consider this code for a few moments to make sure you understand what is happening. First, we have the include statements that you see at the beginning of all C++ programs. The fstream file is where we get the file input stream that allows us to read in a file. Next, inside the main function, we declare a character array named buffer. This is literally a buffer that is used to store the data as it’s read in.

The next line of code that is of interest to us is the while (!myfile.eof()). This is a basic while loop that is using the negation operator (!) on the end of file property of the myfile object. What this code is essentially saying is that “while it’s not true that this is the end of the file, keep looping.” As soon as the end of file is reached, the loop will stop executing. Finally, we see the getline method used to retrieve an entire line of text from the keyboard input. Remember that the getline function allows you to determine how many bytes will be read in, thus preventing you from over flowing an array.


Notice that the file we chose to open in this example has no path, it is in the same directory as the program. If your file is in a different directory, you must use a full path. However, remember that inside strings the \ character denotes an escape key, so to put that character in you have to use a double slash \\. The following is an example. ifstream myfile ("c:\\myfolder\\test.txt");

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net