Sunday, September 26, 2010

fstream in a nutshell



Imagine your little elementary brother asking you to help him with his assignment in mathematics. The instruction of their teacher was to get the average of a set of values. Because you feel the need to help your littler bro out, you glanced at his notebook and saw five sets of numbers to be averaged.

  1. 5 5 6 8
  2. 12 23 34 45
  3. 100 200 300 400
  4. 12 76 45 56
  5. 23 23 34 12
You say to yourself, "Hey, my little bro can handle these questions without breaking a sweat". But because you are a loving older brother/sister who wants to make his little bro's life easier than it already is... you went to your room, looked for your calculator and went back to your little bro's study table to give him the gadget. As you were giving it to him however, you noticed that he already had answered all of the questions, so you say to yourself, "Well I'll be, that's great, guess you won't be needing mr. calculator's help anymore". So you turned around, ready to return it to your room when you suddenly felt a heavy pull on the bottom part of your shirt.

It was your little bro, giving you the pitiful "doggy eyes" look, showing you his notebook as he flips the pages, saying in a desperate voice, "brother, what about the other 500 sets of numbers to be averaged??"...

FSTREAM WANTS TO MAKE YOU LIFE EASIER


Even if we make a program to get the averages of the list of number sets. It's almost expected that after we get a working program, the task is still "strenuous". If the program developed would have no loops we need to
  • compile the program
  • re-run the program 500 times
  • type the number sets 500 times
  • get results and copy them to your brother's notebook 500 times
or even if the program developed already incorporated in it the use of loops, we still have to
  • compile the program
  • enter number sets 500 times
  • get results and copy them to your brother's notebook 500 times
with using fstream functionality however the steps are reduced to
  • write all the number sets in a separate text file
  • compile and run your program once only
  • copy the results to your brother's notebook, (or technically your brother has to do all the copying, right??)


HOW DOES IT DO THAT?


take a look at this sample code below

  1. #include <iostream>
  2. #include <fstream>

  3. using namespace std;

  4. int main()
  5. {
  6. int num1, num2, num3, num4;
  7. ifstream infiler;
  8. infiler.open("assignmentOfLittleBro.txt");
  9. if (infiler.is_open())
  10. {
  11. while (!infiler.eof())
  12. {
  13. infiler >> num1 >> num2 >> num3 >> num4;
  14. //get the average of the four numbers and output it (left as an exercise)
  15. }
  16. }
  17. infiler.close();
  18. return 0;
  19. }
It somewhat works this way, God declared us to be to be of type human, having declared as such we find ourselves capable of some kind of skill or functions like singing, cooking our own meals, using our hands to produce things etc. Now notice that in line 9 infiler is declared to be of type ifstream , being declared as such infiler is granted the capability to function both as a bridge and a container.

How is it a bridge?? Line 10 tells us that infiler has a skill .open() that allows it to open files located in the same folder or directory. Line 11 also demonstrates how infiler checks if the text file to be opened (orange text) was successfully opened using .is_open(). Being multi-talented infiler also was given the talent to check if we've already reached the end of the file we've just opened (Line 13) using .eof(). The while loop is there since we need to iterate through the data present in assignmentOfLittleBro.txt which would look something like this (you're the one to encode it).

5 5 6 8
12 23 34 45
100 200 300 400
12 76 45 56
23 23 34 12

How is it a container?? Line 15 could be translated as "get four integers from assignmentOfLittleBro.txt and store them into four variables namely the ones we've declared". combining Line 15 with Line 13 it would pretty much translate to "until we reach the end of the file get four integers from assignmentOfLittleBro.txt and store them into four variables namely the ones we've declared". Isn't this great??? Now try the following things out...
  • file streaming will not work without #include <iostream> why is that???
  • suppose you have the following input values
10 20 30 40 50
1 2 3 4 5 6 7 8 9 0
123 234 345 456
  • will the code still work?? Why?? Why not??
P.S. - infiler could be any other name, you can have it as ifstream myVariable or whatever you fancy

Goodluck with your little brother! Make him proud. =]

-oOo-

2 <- post your comments here: