reading 3 ints from one line, then 3 from another

Hi, I知 having some trouble getting a program to work. What I知 trying to do is input 3 ints from a specific line in an external .cpp file.
say I need to find out what ints x, y, and z are. the external .cpp file reads:
"
1 10 1900
4 20 2000
6 66 1666
"
what would I do to first read line one, making x=1, y=10, and z=1900, then after using the data, read the next line, making x=4, y=20, and z=2000?
if I write:
infile>>x>>y>>z;
it will only read the last line in the file. making x=6 and so on.
would getline work? and if so, how?
getline can read a specific line, but can only take that line in as a string, not 3 ints.

you need to post code, because the line you posted should work ok. Did you put that line inside a loop that reads to end-of-file? If you did, that would explain why it seems to be only reading the last line, because you tossed each line upon each interation of the loop.

if I write:
infile>>x>>y>>z;
it will only read the last line in the file. making x=6 and so on.

No, all the values are read into x, y, and z, but everytime through your loop x, y, and z get overwritten by the next line of data. If you want to store all the data make x, y, and z arrays and everytime through the loop, increment the array index. That way x[0], y[0], z[0] will store the data in the first line, x[1], y[1], z[1] will store the data in the second line, etc.

In addition, the general rule for reading from a file is: the read statement should be a while conditional, e.g.

Code:

while( inputFile>>data1 )
{
...
...
}

inputFile>>data1 calls a function that reads data into data1 and then returns the inputFile object, which converts the while conditional to:

while(inputFile)

If any errors occur that will prevent the loop from reading data from the file, the inputFile object will evaluate to false in the while conditional. End of file(eof) is considered an error, so the while loop will terminate correctly when all the data is read in. However, there are other possible errors that can occur while reading from a file. If one of those errors occurs, then a loop such as:

Code:

while(!inputFile.eof())
{
}

will try to keep reading data because it hasn't encountered eof yet--but the error will prevent the read statement from reading any data. So the loop will keep looping indefinitely: not able to read in any data because of the error and therefore never reaching eof.

Also, I'd like to mention an issue of preloading your entire file into memory or loading parts through out the program. Now in anything you're writing, since it won't be big, I'd say do what 7Stud says and load it all into arrays. This makes it much quicker when running the program when it's all in memory, especially if your loading a file from a floppy disk.

If you get into databases, though, it's more proper to be selective as to what and how much you get from your file. Rather than putting it all into memory and working with that, you could load in line at a time and do whatever you need to do with that, then repeat for the next line.

As you can see, the user is prompted to input the name of a file that contains several dates, then the program calculates what day of the week each dates is.
Thanks for your ideas. I'll try to use an array in this program for the various dates.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

No need to use an array here, just remove the while loop from the GetDate method. You are already calling GetDate inside a loop in main. You might want to have GetDate check the filestream state to see if it failed and return true or false so that the loop in your main function can stop when the file is empty.