File Handling in C++: Advanced Input

Written by Ali on. Posted in C++

In our last post on inputting information from file, we discussed how to open a file and read from it to input useful information. This information saves a lot of load and helps in quick and easy input of a lot of data instantly into the system for processing. In this post, we’ll show you how to take advanced input; input different data types simultaneously from the file that could help even more in the data processing.

Suppose that we are working on a student record storing program whose input file format looks like this:

</p>
<p>&nbsp;</p>
<p>Name               roll-number        mid1      mid2      final</p>
<p>Jawad Hadi,        4445                29        35         37</p>
<p>&nbsp;</p>
<p>

 

Here we have the facility to input the whole line of information via a string, inputting information till the end of line ‘\n’ (think how you would manage this!) but that would be a mess as we won’t be able to process the information of mid1, mid2 and final in order to calculate total marks and the grade. For this, we have to set the information that’d store the roll-number, mid2, mid2 and final in the int form. And how we’d be doing that, take a peek at the code below.

</p>
<p>int main</p>
<p>{</p>
<p>ifstream fin;</p>
<p>fin.open(“input.txt”);        //input.txt is our source <a href="http://www.nbso.ca/">casino online</a>  input file.</p>
<p>char grade = ‘a’;</p>
<p>int Finalmarks = 0, rollnumber = 0, mid1 = 0, mid2 = 0, final = 0;</p>
<p>char name[30] = {0};</p>
<p>fin.getline(name , 50 , "," );</p>
<p>fin &gt;&gt; rollnumber;</p>
<p>fin &gt;&gt; mid1;</p>
<p>fin &gt;&gt; mid2;</p>
<p>fin &gt;&gt; final;</p>
<p>//</p>
<p>//</p>
<p>fin.close();</p>
<p>return 0;</p>
<p>}</p>
<p>

In the above code, the fin.getline(name , 50 , “,” ); is used to input the name from the file, the input is till ‘,’ which acts as a delimiter character. Any such symbol is bound to be kept so as to inform the system till where the input is to be inserted in a character array data type. After the extraction has been done till ‘,’, there is a space in front. Here, the use to fin assists us as the input would be in formatted form and the space would be ignored. The fin >> rollnumber; would allow us to input the roll number in int data type. So will be inputted the other integer values of mid1, mid2 and final.

Now as the input has taken place, we can ease our processes by calculating the final and the grade using the following code in place of the comments placed in the code above.

</p>
<p>grade = ‘a’;</p>
<p>Finalmarks = mid1 mid2 final;</p>
<p>if(Finalmarks  &gt;= 90)</p>
<p>{</p>
<p>grade = "A";</p>
<p>}</p>
<p>else if(Finalmarks  &gt; 80 &amp;&amp; Finalmarks  &lt;= 89)</p>
<p>{</p>
<p>grade = "B";</p>
<p>}</p>
<p>else if(Finalmarks  &gt; 70 &amp;&amp; Finalmarks  &lt;= 79)</p>
<p>{</p>
<p>grade = "C";</p>
<p>}</p>
<p>else if(Finalmarks &gt; 60 &amp;&amp; Finalmarks  &lt;= 69)</p>
<p>{</p>
<p>grade = "D";</p>
<p>}</p>
<p>else if(Finalmarks  &gt; 50 &amp;&amp;  Finalmarks &lt;= 59)</p>
<p>{</p>
<p>grade = "F";</p>
<p>}</p>
<p>

Thus, you see how easy our work can become upon using the right data types to input the information from a file. This is a small scale operation; imagine dealing with records of hundreds of students and calculating their final marks then assigning them grades accordingly.

Figure out how you will deal with handling such information on a large scale. In our next post, we’ll deal with how to output the useful information on file for later use and safe storage. Till then, stay safe.

Tags: , ,