Quick fstream question

ok so I know how to get data from a file and print out data to a file, but here is my problem let's say I have a line of data that I am going to be assigning to variables I know that I can put a space between them and have different variables on the same line, now let's say that I have 10 lines (data on ten people) and I want a loop to go 10 times once for each line, how do I get the program to look at the second line on the second loop and so on, I've tried looking this up but can't find a clear answer do you understand what I am asking?
Thanks
Shawn
Just put all of the strings from the getlines into an array/vector. Then you can access them by their # (or just iterate through them if you want to do the same thing to each one)
I want them to do the same thing for each one, I've heard of the getline, but couldn't get it to work, I am assuming it would be getline then the variable for the loop, which would tell it what loop it's on, do you mind showing me an example in code?
heres a example of what I am trying to do this would be in the input file

1999 35 76 89 5000 y
2999 45 98 98 6000 n

for loop 1 i want it to process the data from the first line for loop 2 the second line, and so on each time they get the same variables, I just dont know how of the second loop to get it to skip the first line
If you getline with a stream, it (by default) will read till the first newline and automatically move the file pointer forward for you, so you can just use it repeatedly and not worry (at least not until the end of the file).

Example:

1
2
3
4
5
6
7
8
fstream File;
string buffer;
vector <string> data;
File.open("file.txt");
while(File.good()) {
   getline(File, buffer);
   data.push_back(buffer);
}
Last edited on
ok, i must be missing something, why would it go to the next line in a loop if the code is exactly the same, that is what i am not understanding, I have literally being trying to figure this out for hours, so what would I type, just getline() and this will get a new line each loop? I cant see how this would work at all, it doesnt help that my teacher doesnt really accept questions at all, his answer is always " go figure it out"
Ok, so basically how streams work is they have "pointer" to where they current are in the stream (file in this case). Everytime you call getline(), it will move it forward, so the next time you use the stream with getline(), it will start at the pointer (which is at the next line) and get the next line.
Topic archived. No new replies allowed.