File Handling in C++: Input Method

Written by Ali on. Posted in C++

File handling is an important feature in C++. File handling has a basic purpose to input and output information from a source or to a destination. What file handling basically assists in is that it saves efforts to input a large amount of data every time you run a program.   File handling consists of reading and writing with any type of file, but in this post, we will keep ourselves to text files with .txt extension in Windows. 

Suppose you are loading a student information record inside a program, it’d be really hectic to load the information of 50 students every time you run the program. To make stuff easier you load all that information instead of getting the input every time. That is where the file handling kicks in. Now to make your program allow file handling, you have to insert the fstream header at the top of your code.

</p>
<p>#include &lt;fstream&gt;</p>
<p>

This allows the filing stream to be opened and you to perform file operations. File operations are of two types: Input and output.

For inputting information from file to the program, you have to make an variable of ifstream class: the variable acts more like an object of ifstream, where ifstream is used for input from stream. For outputting information to the file so that it can be loaded later for program use or other purposes, you have to make a variable of ofstream, that’d again be more like an object of ofstream, where ofstream is used for outputting information to a file. You are familiar with normal input and output streams, cin and cout of istream and ostream classes respectively. So the streams of fstream class are similar to these streams.

In this article we talk about input, more specifically ifstream. To allow input, you have to place the input file inside your project folder, or specify the complete path for it. To make things easy, it is better to put it inside the project folder. Then comes the part where you have to open a text file to start inputting the information from it. Also when you are done inputting from a file, it is imperative that you close it as well to save it from any future errors during the running of the program. Refer to the following piece of code:

</p>
<p>&nbsp;</p>
<p>#include &lt;fstream&gt;</p>
<p>using namespace std;</p>
<p>&nbsp;</p>
<p>int main()</p>
<p>{</p>
<p>.  char temp [50] = {0};</p>
<p>.  iftsream fin; //could be any name, but keep it similar to cin</p>
<p>.  fin.open(“input.txt”);    //this is where the input file stream is opened and the name of the file comes in double quotes.</p>
<p>.  if(fin.is_open())                                //to check if the input file is opened or not</p>
<p>.  {</p>
<p>.   cout &lt;&lt; “The input file is now open”;</p>
<p>.  while (!fin.eof())</p>
<p>.   {</p>
<p>.    fin.getline (temp, 50) ;</p>
<p>.    cout &lt;&lt; temp &lt;&lt; endl ;</p>
<p>.   }</p>
<p>.  }</p>
<p>.  else</p>
<p>{</p>
<p>.   cout &lt;&lt; “The input file failed to get opened”;</p>
<p>}</p>
<p>.  fin.close;              //this closes the opened file stream</p>
<p>.  return 0;</p>
<p>}</p>
<p>

 

First of all we declare an object of ifstream class. ifstream class is defined in the fstream library. After declaration we open the file and associate the file with the stream. Then we read the data from the file with similar method we use with normal input. The difference is just with the name fin. It is good to place a check whether the file has been opened or not. It is done with a built in function fin.open(), that opens up a file in background processes . We take input until end of file is reached which is checked by fin.eof(), where eof is ‘end of file’. As there is no other way to move the pointer down the file, we have to set this function to make the cursor move down and in a while loop it will continue to input information in some respective order until it has reached the end of file.

Now, you have to input the information from the file in some specific order. For the time being, we consider that there is a paragraph stored in our “input.txt” file. We take input in a temporary variable, using getline function. Getline function is used because the paragraph would have spaces in between, and thus input would be in the form of formattted input and getline() function would be used instead of get(). As you would have seen, the getline function gets two parameters, in fact it can has 3 parameters as well. The first parameter is destination, the second parameter is the size of the input to be taken and the third parameter is a character that can be set as delimeter mark. the delimeter mark is by default set to ‘ \\n ‘ in getline functions but it can be changed by sending the third parameter. The above program reads the data and stores into the temporary character array and then outputs it on the screen.

That’s all for now folks, next time we’ll be back with more on file handling. Till then, “Hello World!”.

Tags: , ,