A sequential file is simply a file where the data is stored one item after another. The flat text files you have dealt with so far are sequential files. A more technical definition would be that a sequential file is a collection of data stored on a disk in a sequential, nonindexed, manner. C++ treats all such files simply as a sequence of bytes. Each file ends with a special end of file marker. Sequential files do not have additional inherent structure. Any additional structure must be imposed by the program reading and writing the file. What this means in plain English is that there is no way to move around in a file. You just start at the beginning and input the entire file from start to finish. You can open these files in a variety of ways. Of course, you will still have to include ifstream and/or ofstream.
Step 1: Declare a variable of type ifstream/ofstream, depending on whether you need input or output. This variable is often referred to as a file handle.
ifstream infile; // declares file handle called // infile ofstream outfile; // declares file handle called // outfile fstream inoutfile; // declares a file handle that // can be either
Step 2: Open the file for reading, using the ifstream member function open(). When you open it you pass it the name/location of the file you wish to open, and the mode you wish to open it in. The mode tells it whether this is input, output, or both.
infile.open("myfile.txt", ios::in); //for reading only outfile.open("myfile.txt", ios::out); //for writing only inoutfile.open("myfile",ios::in|ios::out); //for reading //& writing outfile.open("myfile.txt", ios::app); //for appending outfile.open("myfile.txt", ios::trunc);
This last one is rather interesting. The truncate option says that “if the file already exists, then wipe it out and start with a new file.” You should, obviously, be quite careful with this one.
When you need to go to file that is not in the same directory/folder as your program, you will need to specify the path.
Watchout! | Remember that the \ in C++ denotes an escape character. So you must use \\ to denote directories. The following is an example of the wrong way and the right way to do this. WRONG: infile.open("c:\myfolder\myfile.txt", ios::in); CORRECT: infile.open("c:\\myfolder\\myfile.txt", ios::in); |
Let’s look at an example that combines all the major modes for opening a file. This will let you see how each of them work.
Step 1: Enter the following code into your favorite text editor.
#include <fstream> #include <iostream> using namespace std; int main() { char buffer[256]; // open it for output then write to it fstream myfile; myfile.open("test2.txt",ios::out | ios::trunc); if (myfile.is_open()) { myfile << "This outputting a line.\n"; myfile.close(); } // open it for input and read in myfile.open("test.txt",ios::in); myfile.getline(buffer,100); cout << "The file contains " << buffer << "\n"; myfile.close(); //open for appending and append myfile.open("test.txt",ios::app); myfile << " Hey this is another line \n"; myfile.close(); // open for input and print to screen // open it for input and read in myfile.open("test.txt",ios::in); myfile.getline(buffer,200); cout << "The file contains " << buffer << "\n"; myfile.close(); return 0; }// end of main
Step 2: Compile the code.
Step 3: Run the code.
In this single example, you can see a file object being opened several times in several different modes. This should illustrate to you the various uses of the modes for opening a sequential file.