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


not the end of the file, it will return false. So if we are in the middle of the file, it will return false ( its not the end of the file ), but I want it to go through, so I need to make it true ( ! will give me the opposite ). Think of it " While it's not the end of the file, do this... ". Thats all I can really say about that. I would recommend that you use this whenever getting information from a file, because you never know how big a file is.
One thing before we go on. Now, thats all find an dandy if we are getting an int to an int variable, and if we are getting a double to a double, but what happens when they are missed matched ? For instance, lets take a sample file, called test.dat


---> test.dat <---

---------------------------------
18 14.2 NAME
---------------------------------


Lets say that is our file that we have to work with. Lets write a small segment of code:

ifstream InFile;
int intvar;
double doublevar;

InFile.open("test.dat");
InFile >> doublevar;
InFile >> intvar;

cout << intvar <<" "<< doublevar;


Now, as you can see, when we open the file, the first piece of data is an int, and we are storing that as a double, so it would act like we just type-casted it, so doublevar would have the value of 18.0 The intvar however, will not take the decimal, it will only take the 14, and leave the .2 there. So the value in intvar would be 14. Now, lets say that we had a char variable, and we did the same thing, you would think that it would have the value of 'B' right ? WRONG ! It would have the value of '.' YOu know why ? When we got the 14 out of 14.2, it stopped at the decimal place, and left the CP there, so, the next data piece was a character, and that is what it got. Basically, if it is possible, the value will be type casted into whatever the variable is. So if the value extracted from a file is an int, and we are storing it in a double, we are typecasting it to a double, and vise-versa. Same with all data types. That is something to watch out for when beginning, and a fundamental technique that is commonly overlooked, but I feel must be learned.

Now, we have had fun with the basics of input and output streams with files. But we are not done yet, oh no, we have only cut a thin layer, there is a lot more to be learned. Now, I can not go over EVERYTHING, but I will cover with the basic stuff.

For instance, lets say that we wanted to get a character, just one single character from a file, or put one single character in a file. There are two member functions for input/output file streams, called GET() and PUT(). Now, lets look at a quick example. Lets assume the following file is valid


---> test.dat <---
---------------------------------------
My Name is Bob
---------------------------------------


Now, lets write a quick little program for this.

ifstream InFile;
ofstream OutFile;
char tmpchar;
int spaces; // Number of spaces

InFile.open("test.dat");
OutFile.open("testresults.dat");
while( !(InFile.eof()) )
{
   InFile.get(tmpchar);
   if(tmpchar == ' ')
      spaces++;
   else
      OutFile.put(tmpchar);
}
OutFile << " Spaces: " << spaces << endl;

Now, lets examine. First off, as we always do, declare our stream variables. Then we have a temporary character variable, and a spaces counter. Then we open a input file, that has the data, then open an output file, so record the results. Now, when we first open the file " test.dat", we are at the first CP. Whe we call InFile.get(tmpchar); all that is basically doing is taking the next character, and putting it in the variable tmpchar, so the first time, tmpchar is 'M', then it is 'y' then it is ' ' and so on. Now, OutFile.put(tmpchar); will simply put whatever character is in tmpchar in "testresults.dat", if and only if it is NOT space. When we are done, we output how many spaces there were in that sentense. This is a point less little segment, but all it does it copy the sentense with no spaces, then write down how many spaces there were. Just an example to show you how to use PUT and GET.

We are almost there, there are a couple more things that I want to share with you, things that you can do, easy things just to tie things up. Now, first thing is first, the entire time, we have been doing this.


ofstream OutFile;
OutFile.open("filename.ext");
Well, we can compress that into one line, like this

ofstream OutFile("filename.ext");
Its just a simpler way to do things, but its up to you, works the same either way. Also, we may want to have the user specifiy a file name, wouldnt that be nice ? Well, all we have to go is use a C-Style string to open it. For example.

ofstream OutFile;
char * filename;
cout << "Input file name, with extention ( i.e. filename.ext ): ";
cin>>filename;
OutFile.open(filename);


And thats that. Just make sure the file name is valid, and then you are good to go. One last thing before we leave this chapter, FUNCTIONS !

Yes, everything we just learned, can be tied in with functions. ifstream and ofstream are both data types, there for can be used at parameters in functions. And we treat them the same way, the only thing is, that is HAS to be a reference parameter. Lets look at this, just to clear it all up:

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

int GetInteger(ifstream &); // Declaring function

void main()
{
   ifstream InFile("integers.dat"); // Lets assume a file of int's
   int total;
   while(!(InFile.eof()))
   {
      total = GetInteger(InFile);
   }
   cout<<" The Total was: "<<total<<endl;
}

int GetInteger(ifstream &LocalInFile)
{
   int temp;
   LocalInFile >> temp;
   return temp;
}


Now, there is the first full program I have written since chapter one I think. That is an example of the input/output file streams as parameters. When we open the file, "integers.dat", and we pass in the stream to the function, we are basically passing in the file almost, we are just not re-opening it, because it is already open from the main function. Just remember, that when we are passing files, its HAS to be a reference parameter.

Well, I must say, this, so far is the longest tutorial I have written. And I am sure it will remain that way until OOP starts to kick in, which I believe is next chapter, so it might be a little while. But fear not, OOP is the fun part of programming, especially in C++. It is at least in my opinion. Until next time, Happy Programming !


C++ - OOP, Part One


Welcome to the power of C++. In this tutorial, we will examine the main reason why C++ is so much more powerful than other programming languages today. This will be a multiple tutorial lesson ( OOP that is ). So prepare for a lot of reading, and trust me, it will be worth it.

First off, lets start by explaining what an OOP stands for and what it means. It stands for ObjectOrientedProgramming. Now, what does that mean ? Just what it says. When we create programs, we will be programming with objects. Now, I bet your asking, what is an object ? Good question.

Objects are nothing more than a user defined data type. You already know that int's, double's, float's and others are built into the compiler for you, so you dont have to mess with them. But, its harder when you want to use variables to represent other things. Now, I bet you ask, why on the earth would I want to do that ? IT MAKES LIFE EASY !

Now, I am going to start off with an example, and you will know what I mean. Lets start with a basic ( used in C ) object, called a structure. What I would do is this:

struct Student
{
int id;
double gpa;
};

Student CPPGOD; // Thats me ! hehe


Now, thats not to bad, and im sure you saying " Why not just make two variables, and call it a day ". True, we could do that, but lets put this on a larger scale. Lets say instead of you doing just one student, you have to do an entire class ( around 30 students ) or the entire school (around 1200 students ) or the entire county ( depends on size, around 20000 students ). In each case, if we were to make two variables for each and every student, we would be there for a week, just initializing the variables. That would be 60 variables just for the one class alone ( imagine the 40000 for the entire county ). Now you get my point ?

Page : << Previous 8  Next >>