I posted a problem similar to this a few days ago, but the help I got didn't quite work the way I thought it would, so I'm trying a different approach now...
This code needs to be able to specify what line in the file the getline will get the string from:
1 2 3 4 5 6 7 8 9 10
//Assume all includes etc are declared and the program works otherwise.
ifstream in;
string my_data;
in.open("the_file.txt");
//SPECIFY WHAT LINE GETLINE STARTS AT
getline(in, my_data);
cout<<my_data;
I noticed that there is a member function for ifstreams called seekg() with which you can specify where the getline will start from, but you can't use it to start at the beginning of a line unless you have prior knowledge about what the file contains.
It's possible to use it with text file but better forget it. Consider storing your data as binary data with all records having the same length, then you will be able to easily calculate the position of n-th line.
I have made storing strings of different length in a binary file in the form
len_of_next_line, next_line_itself, len_of_next_line, next_line_itself, ...
Then you can skip from line to line what is faster than reading all previous contents.
Or you can create a binary file, where some reserved header bytes contain the addresses of all records in the file.
But if the file is not very large, there is no problem to read it all to find a wanted line.
Yeah, I had the file formatted so every line was the same length, and that's how I was doing it. I was just wondering if there was a more 'universal' way of doing this. Doesn't really matter, though. This works. Thanks