Thursday, April 19, 2012

Using the stream insertion operator to read files

I happened to run across a code example for reading text from a file as follows:



int i; 
char *fileName = "text.txt";
ifstream fin(fileName);

while (fin >> i)
{
do something;
}


This code actually opens and reads space-delimited text, but I don't understand how it works. How does the file get opened without an "open" or "read" command? Is there a way to rewind back to the beginning of the file? I'm trying to create a dynamically allocated array, which I don't know how to do until after I've counted through the file for the number of scores.





No comments:

Post a Comment