Digital Reference

Introduction to programming --- C++

FILES
Files:

Files I/O is basically the same as I/O streams. The difference is, instead of passing through the standard input and output devices (monitor and keyboard), file streams get input from files, and output to files. The library fstream.h should be included to handle files. Filestream classes are derived from the iostream classes, therefore they have all the basic properties of the iostream classes. This derivation is called inheritance in C++, and it is one of the powerful features of object-oriented programming.

How do you create a file? Let’s look at the example:

ifstream newfile; //the ifstream class defines the type of newfile
newfile.open("filename.ext"); //load filename.ext to newfile
                                           //if it does not exist, create it

If you don’t want the program to create a new file, add ios::nocreate:

newfile.open("filename.ext", ios::nocreate);

There’s also a function to detect errors, fail() so:

if (newfile.fail()) {

    do something

}

would detect for opening file errors. When you finish with a file, always remember to close it: newfile.close();

File streams:

So well yes, but how do you read from and write to a file? Examine the following example:

Let’s say we have a file numbers.txt that contains these numbers:

0 2 6 9 4 3 5 7

we want to read them to the screen, then write the word "done" to the file.

#include <iostream.h>
#include <fstream.h>

int main()

{

    ifstream fromfile; //makes fromfile like a cin
    fromfile.open("numbers.txt", ios::nocreate);
    if(fromfile.fail()) {
         cout<< "Error processing file";
         return 0;
}else {
    int num;
    while (fromfile >> num) //process file
    cout << num << " ";
    fromfile.close();
}
    ofstream tofile; //makes tofile like a cout
    tofile.open("numbers.txt");
    tofile << "done";
    tofile.close();
    return 0;
}

I think the only tricky part of this program is the line:

while(fromfile >> num)

Before it confuses you further, please know that NORMALLY when you read a file, you have to read it sequentially, which means you cannot read from the end to the beginning, or from the middle to the end, you have to do it character by character, word by word, or line by line from the beginning to the end. So what fromfile >> num does is, it gets one num (the first integer) from the file. If you do fromfile >> num again, it would get the integer following the last processed integer, and so forth. So in our example, the first fromfile >> num would assign 0 to num, then if somewhere later in the program you do fromfile >> num again, it would assign the next integer, which is a 2, to num. Therefore, the while loop was used to do this again and again until it reaches the end of the file, and returns false (because there isn’t any more integer to process in the file) to the while statement, thus the loop stops. Another way to do this is to use the eof() member function, it stands for end of line. It returns a Boolean value telling you whether or not you reached the end of file.

while(!fromfile.eof()) {

    fromfile >> num;

}

And remember, fromfile acts just like cin, and tofile is just like cout, since they are inherited from the ios class. So all those getline(), width(), and such functions are usable with file streams just as they are with io streams.

Random file access:

Well many people don’t like to follow normal rules, so what if you don’t want to follow the sequential order of file access? There are a few member functions in the ifstream and ofstream classes can help you do this. They are:

seekg(int) //jump to the (n+1)th character in a file for reading

seekp(int) //jump to the (n+1)th character in a file for writing

tellg() //tell where in the file we are for reading

tellp() //tell where in the file we are for writing

They are used with the dot notation (because they are member functions), so:

fromfile.seekg(4) will jump to the 5th character in the file, then start reading there. Why 5th instead of 4th? because it’s like arrays, it starts with 0. The rest of the functions work the same way. One option you might want to know is the ios::end

fromfile.seekg(0, ios::end);

would jump right to the end of the file. 

Remove() and Rename()

As their names imply, these two functions do just what they say, remove() removes a file from your harddisk, and rename() renames a file from your harddisk.

Examples:

remove("filename.ext");

rename("name.ext", "change_to_name.ext");

That’s about it for files.

 

NEXT

Home
Overview
Variables
Conditions
Loops
Arrays
Functions
Files
User-Defined
Pointers
Credits
BBS Discussion
Search
Be Informed: Join us

Subscribe
Unsubscribe 
 
 

 

 

 

 

 

 

 

 

 

 

 

ThinkQuest Team C008294