O.K., this week let's start with something simple and easy. Assuming you know your simple screen input/output via cout and cin, you should have no problem with file input/output.
First, let's include the header file for file i/o:
Code:
#include <fstream.h>
Now, lets look at the two important types of fstream we will use: ifstream (InFileStream) and ofstream(OutFileStream). You will use these to declare the internal filenames for the program. The file you are recieving input from will be designated by ifstream, and the file you are outputing to will be designated by ofstream.
Code:
int main()
{
ifstream fin;
ofstream fout;
return 0;
} Here I will use the common fin and fout so you can feel comfortable and see the similarity between them and cin and cout. You can use any variable names, however, and I commonly use read and write myself.
Now that we have established the internal filenames, we need to specify external filenames (what they are called on the system) to connect them to.
Before we start, make a txt file named "input.txt" and enter the numbers on the first line like this:
12 32 43 32 11 10
Now, let's connect fin and fout to appropriate files.
Code:
int main()
{
int x;
ifstream fin;
ofstream fout;
fin.open("input.txt");
if( fin.fail() )
{
cout << "File not found! Now exiting!" << endl;
exit(1);
}
fout.open("output.txt");
if( fout.fail() )
{
cout << "File error!" << endl;
exit(1);
}
return 0;
} Now, notice the if statements. The fin.fail() usually is true when the input file does not exist, so we need to exit if it is true, otherwise we will have some nasty problems. It may also evaluate to true under other circumstances, which fout.fail() may evaluate to true if your hard drive is tightly packed to it's limits!
Now, let's do something already!!! Add the following after the fout if statement and compile it. Compare what you see on the screen and what you now have in the output file.
Code:
fin >> x;
cout << x << endl;
fout << x;
Repeate these three lines 6 times, or even put them in a for loop like the following:
Code:
for (int i = 0; i < 6; i++)
{
fin >> x;
cout << x << endl;
fout << x;
} You've just coppied the contents of the file! Now try using fout as you would cout. For example:
Code:
fout << "This is another lame Hello World output." <<endl;
Try using other functions you are familiar with in the cout/cin operations, such as getline and such.
Now, before we go any further, lets not forget the very important function close. It's always good technique to close files once you are finished with them, so you should include either of the following either when you are done with a file (in the case where you use multiple files) or at the end right before return 0;
Code:
fin.close();
fout.close();
Now, let's look at some Input/Output functions we can use. consider the following to replace our earlier for-loop:
Code:
while ( ! fin.eof() )
{
fin >> x;
cout << x << " ";
fout << x << " ";
} You may notice that the last number from the file is taken twice. Why? This is one problem for you to try to solve.
Well, that wraps it up for tonight. My next posting will be tomarrow to continue this out. For practice, take what we have done so far and try implementing different input/output functions you know for cout and cin, and try using different types of data (ints, char, strings, etc.).
Also, use a user defined filename instead. For example, use a string for the user to enter, then use that string as the filename.
Code:
char filename[16];
cin >> filename;
fin.open(filename);
And, if you know how to sort, try sorting the numbers from the input file and storing them sorted in the output file.
And, for those interested, here is a link to some of the member functions fstream uses if you want to explore some other functions I may not touch.
http://www.cplusplus.com/ref/iostream/fstream/
If you have any questions or problems, post it in this thread and I will respond before my next post.
