Topic : C++ Tutorial
Author : Unknown
Page : << Previous 7  Next >>
Go to page :

the local file, "user.dat". Now, knowing that this is a computer, there is always the chance that an error of some kind has occurred. So, there is a method to check and see whether or not the file was opened correctly. fail() will check to see if the file was opened and our stream is valid. Lets look at this example:"user.dat");
if( )

Now, when is called, it will return an integer, 0 if it was successful and a negative if it was not open successfully. So, when it goes through the if statement, it was not opened successfully, then it will end the program. So, lets assume that the file was opened successfully, and then we get to have fun. Now, we have programmed before with cout, which is the stream to the default display adapter ( monitor ). Well, when we made our stream to the file, named OutFile, we can just use the same things that we used with cout. Now, remember how when we did something like this:

cout << " Hello Screen " << endl;

That would write " Hello Screen " to the screen. We can do the same things with files. We can do this:

OutFile << " Hello File " << endl;

To write to the file. Now, if we opened the file, user.dat, we would see, " Hello File ", and thats it. When we have the output file stream, it has very many similair properties of the stream to the monitor, mainly because they are both of kind output streams. So lets look at a quick code segment:

ofstream OutFile;
int userage;"user.dat");
cout << " What is your age: "; // Gets program user's age
cin >> userage;
cout << " You are " << userage << " year(s) old " << endl; // Writes age to screen
OutFile << " You are " << userage << " year(s) old " << endl; // Writes age to the file

Now, this is mostly review, except that last line. We make an output file stream variable, the an int to get the users age. Then we get the user's age, and write it to the screen ( cout << " You are " << userage << " year(s) old " << endl; // Writes age to screen ) and then also to the file ( OutFile << " You are " << userage << " year(s) old " << endl; // Writes age to file, "user.dat" ). Thats pretty much it for output to files, now, lets cover input with files, which will probably take a longer time than output. But we will see what happens.

One thing, before we learn about input streams. For both output and input streams, when your program is done, you HAVE to close the stream, by using the method, close(). This is how it is used

ofstream OutFile;"filename.ext");
// do whatever

Thats all, just rememeber to do that at the end of all of your programs, and you will be ok.

Now, we know what the output stream does, so what does the input stream do ? Well, it will get information. Duh ? cin is a perfect example of an input stream. When we do something like cin >> age;, that is an input stream from the program to the user. When we input a number, it will put that in age. Now, when we declare a file input stream, we will obtain information from the file. Now, first thing that we have to do, just like output stream, is actual make one ( declare a variable ). We do that just like this:

ifstream InFile; // Input stream
Now, this will open an input stream, but like output, we have to connect it to a file, and we do that by the same method, open(). Just like this:"user.dat"): // Connect input stream to file, " user.dat "

Now, we have an input stream to the user.dat file, that we can obtain information from. Now, just like output stream, we also have a fail method to make sure that it was opened correctly, which works the same way, see above ( click here )

Now, we have a file opened, with no errors, hopefully :) So you ask, "How do we get information from it? " Well, read on, and thou shalt learn.
Remember cin ? Well, we had the operator >>. Guess what ? WE are going to use that operator to get information from the file. Lets look at an example:
Lets say that a file, named "user.dat" had the following information in it.

---> user.dat <---
19 18 21

Lets write a program to extract that data to our program. First off, lets look at the actual code. And lets develop some kind acutal, semi-real life assignment. Lets say that those represent something, say 19 is the users age. 18 is the age to register for something, and then 21 is the age to by beer. Lets dive in:

ifstream InFile; // Input stream
int userage, registerage, beerage; // Will hold data"user.dat"); // Links InFile to file, user.dat
cout << " Extracting data...";
InFile >> userage; // Gets user's age
InFile >> registerage; // Gets age to register
InFile >> beerage; // Gets age to buy beer
cout << " You Can..." << endl;
if(userage > registerage>
   cout << "Register" << endl;
if(userage > beerage)
   cout << "Buy Beer" << endl;
cout << "And thats it" << endl;

Now, thats quite a bit of code, but lets go through it, and then it will all make sense when this paragraph ends. Ok, the first two lines, are just declaring variables, we want the input stream, and the three variables to hold the data in the file. The next line, opens the file "user.dat" and connects it to our data stream, InFile. Then we output a message to the screen, telling the user that we are extracting the data. Then we get into the fun part. Before I explain the next three lines, I have to explain a concept called Current Position (CP). When we get data from a file, we are getting what data is based off of the current position. Now, when we open the file for the first time, the CP is at the first line, first character ( think of a point, (0,0) if you will ). Then from that point forward, if we extract any data from the file, the CP is moved. So when we extract the userage, we do InFile >> userage;. That will store 19 in the variable userage, and then it will move the CP over to get right of 19, to get ready for the next input. Then when we get the registerage, 18 is stored in the variable, and the CP is moved over, and the same thing for beerage. See how that is done ? Well, as of right now, we cant control where the CP is, but we will be able to later, I will explain that later on. So, lets think of that great world where everything is in order for right now. Ok, another note, when we use the >> operator for InFile, it has the same properties as the cin ( because they are both istreams ( input streams ) ). It will read up until a space, then stop. So, without further ado, lets continue. The rest if just Chapter 2 stuff, outputing stuff based on what the user's age is.

Now, above, I said that we are going by this little standard that everything will be in order, but I never said that a data file would have a length limit. So guess what, lets assume that we were just given a data file that has random numbers in it ( spaced ) and we dont know how many numbers there are, and we need to get the average of them ? Well, lets look at a quick example, and see how to do that, shall we ?

ifstream InFile; // Input stream for file, numbers.dat
int NumofNumbers; // Will hold number of numbers in the file, numbers.dat
float Total; // Will hold the total for the numbers in the file
             // made float, to hold larger numbers"numbers.dat");
while( ! (InFile.eof()) )
InFile >> temp;
Total = Total + temp;

cout << " Average: " << double(Total / NumofNumbers);

Now, thats not to hard to grasp. Lets look at it, First off, we declare our input stream. Declare necessary variables, then open the file. Then, we see that wonderful while loop that looks greek right now. Well, we covered a while loop, and it will check to see if what is in the while statement is true, before anything happens. Well, that wonderful method, eof() will return a bool. EOF stands for End-Of-File, so remember it, you might see it again. If it is the end of the file, it will return true, if its

Page : << Previous 7  Next >>