Reading lines of a text file

This is a discussion on Reading lines of a text file within the C++ Programming forums, part of the General Programming Boards category; Hi,
Is there a way to read the contents of a text file line by line? For example, I have ...

Reading lines of a text file

Is there a way to read the contents of a text file line by line? For example, I have a text file which has two numbers on two different lines like this:

4
5

...is there a way in C++ to load the text file, and then take the '4' from line one and assign it to a variable, and then read the '5' from line two and assign it to a separate variable? e.g:

Code:

// Code to open text file and read the values of lines one and two.
int line1, line2;
line1 = textfile.line1;
line2 = textfile.line2;
cout << line1 << " is the first line of the file, and " << line2 << " is the second line." << endl;

Failing that, is there a way to define variables in a text file, for example if I have a text file with the following contents:

variable1 = 4
variable2 = 5

...is it possible to define variable1 and variable2 in a program, open the text file and then assign the values given to variable1 and variable2 in the text file to the variables in the program?

You can do the exact same thing using your file stream object in place of the cin. That would work provided your file contains just those two values regardless of whether they were on one line (white space separated) or on separate lines.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens