Sunday, March 21, 2010

File I/O streaming in C++ Introduction

Introduction: This tutorial will start with the very basis of File I/O (Input/Output) in C++. After that, I will look into aspects that are more advanced, showing you some tricks, and describing useful functions.

You need to have good understanding of C++, otherwise this tutorial will be unfamiliar and not useful to you!

Your Very First Program

I will first write the code, and after that, I will explain it line by line. The first program, will create a file, and put some text into it.


#include
using namespace std;


int main()
{
ofstream SaveFile("filename.txt");
SaveFile << "Hello World!";
SaveFile.close();
return 0;
}


This program will create the file filename.txt in the directory from where you are executing it, and will put “Hello World!” into it.

Here is what every line means:

#include - You might have already known the use of this. It includes functions from specified header file. In this file, are declared several classes, including ifstream, ofstream and fstream, which are all derived from istream and ostream.

ofstream SaveFile("filename.txt");
1) ofstream means “output file stream”. It creates a handle for a stream to write in a file
2) SaveFile – that’s the name of the handle. You can pick whatever you want
3) (”filename.txt”); - opens the file cpp-home.txt, which should be placed in the directory from where you execute the program. If such a file does not exists, it will be created automatically.
Now, let’s look a bit deeper. First, I’d like to mention that ofstream is a class. So, ofstream SaveFile(”cpp-home.txt”); creates an object from this class. What we pass in the brackets, as parameter, is actually what we pass to the constructor. It is the name of the file. So, to summarize: we create an object from class ofstream, and we pass the name of the file we want to create, as an argument to the class’ constructor. There are other things, too, that we can pass, but I will look into that, later.

SaveFile << "Hello World!"; - "<<" looks familiar? Yes, you’ve seen it in cout <<. This ("<<") is a predefined operator. Anyway, what this line makes, is to put the text above in the file. As mentioned before, SaveFile is a handle to the opened file stream. So, we write the handle name, << and after it we write the text in inverted commas. If we want to pass variables instead of text in inverted commas, just pass it as a regular use of the cout <<. This way:
SaveFile << variablename;

SaveFile.close(); - As we have opened the stream, when we finish using it, we have to close it. SaveFile is an object from class ofstream, and this class (ofstream) has a function that closes the stream. That is the close() function. So, we just write the name of the handle, dot and close(), in order to close the file stream!

Notice: Once you have closed the file, you can’t access it anymore, until you open it again.

That’s the simplest program, to write in a file. It’s really easy! But as you will see later in this tutorial, there are more things to learn!


Reading From A File:

You saw how to write into a file. Now, when we have filename.txt, we will read it, and display it on the screen.

First, I’d like to mention, that there are several ways to read a file. I will tell you about all of them later. For now, I will show you the best way.


#include
void main()
{
ifstream OpenFile("filename.txt");
char ch;
while(!OpenFile.eof())
{
OpenFile.get(ch);
cout << ch;
}
OpenFile.close();
}


You should already know what the first line is. So, let me explain you the rest.

ifstream OpenFile("filename.txt"); – I suppose this seems a bit more familiar to you, already! ifstream means “input file stream”. In the previous program, it was ofstream, which means “output file stream”. The previous program is to write a file, that’s why it was “output”. But this program is to read from a file, that’s why it is “input”. The rest of the code on this line, should be familiar to you. OpenFile is the object from class ifstream, which will handle the input file stream. And in the inverted commas, is the name of the file to open.

Notice that that there is no check whether the file exists! I will show you how to check that, later!

char ch; - Declares an array of type char. Just to remind you- such arrays can hold just one sign from the ASCII table.

while(!OpenFile.eof()) – The function eof() returns a nonzero value if the end of the file has been reached. So, we make a while loop, that will loop until we reach the end of the file. So, we will get through the whole file, so that we can read it!

OpenFile.get(ch); - OpenFile is the object from class ifstream. This class declares a function called get(). So, we can use this function, as long as we have an object. The get() function extracts a single character from the stream and returns it. In this example, the get() function takes just one parameter- the variable name, where to put the read character. So, after calling OpenFile.get(ch) it will read one character from the stream OpenFile, and will put this character into the variable ch.

Notice: If you call this function for a second time, it will read the next character, but not the same one! You will learn why this happens, later. That’s why, we loop until we reach the end of the file! And every time we loop, we read one character and put it into ch.

cout << ch; - Display ch, which has the read character.

File.close(); - As usual, we need to close the file atlast using this close() function.
Notice: Once you have closed the file, you can’t access it anymore, until you open it again.
That’s all! I hope you understood my comments! When you compile and run this program, it should output:

“Hello World!”

If you guys are much interested in this, then you have two more topics called Managing I/O streams and I/O flags. Have fun reading C++.

1 comment: