Thursday, November 14, 2013

C++ understanding fstream

My brain choked the other day when dealing with the ifstream method inside of the the fstream directive.  I just couldn't wrap my brain around how the ifstream associates variable values.  My friend told me that I was an idiot.  He explained that ifstream works the same way that cin works.

basically
lets say you have a text file that is called names.txt and the file looks like this
Johnny Bobby Dave Suzy
Now you want to assign those values to variables using ifstream

Using 4 variables (name1, name2, name3, name4)  you'd just say
ifstream inputFile;
inputFile >> name1 >> name2 >> name3 >> name4;
Just like it would look with cin
cin >> name1 >> name2 >> name3 >> name4;
 C++ doesn't care if the input is coming from a text file or a keyboard.  Duh.

No comments: