okay I need to write a program that will basically be pulling strings from a text file based on what the user inputs. I can make a program run that gets the text file to input to the screen but I am having trouble with jumping around the file. example:

user requests data that is on line 1 of the file.
(Works)
user then requests data that is on line 10 of the file.
(Doesn't work)

I tried using seekg but it only seeks character by character on a single line.
How do I tell seekg to go to the next line.

Also there will be a good amount of data in this text file and requests for different lines really often, is there a better way to design this program? Keep in mind it must implement the reading from file, the point of the program is that the text files can be switched out with similarly spaced but differently written text files.

In summary, what would be a good way to design a program that allows strings from ridiculously different places in a text file to be easily found and written to the screen, they don't even really need to be stored in variables. It sounds easy but for some reason I just can't do it.

C'mon people I don't want you to do my work for me, just give me some help with ifstream!! Why is no-one replying this would take you like 8 seconds...probably.

I would probably create a loop which uses the getline() function to retrieve each line from your text file in turn and place the data into a variable. If you want to retrieve the data in line 10 for example have your loop cycle 10 times at the end of which the data held by the variable will that held in line 10 of your text file.

I have no idea whether this will work and cannot provide code as I am relatively new to C++ programming. However I'm sure something along these lines would hold your answer.

I would probably create a loop which uses the getline() function to retrieve each line from your text file in turn and place the data into a variable. If you want to retrieve the data in line 10 for example have your loop cycle 10 times at the end of which the data held by the variable will that held in line 10 of your text file.

I have no idea whether this will work and cannot provide code as I am relatively new to C++ programming. However I'm sure something along these lines would hold your answer.

Thanks I will try that, I already saw some code doing something similar, I just don't fully grasp why getline grabs a new line each loop iteration and why I can't make a statement that does this sans loop.

> C'mon people I don't want you to do my work for me, just give me some help with ifstream!
Lesson number 1 - Don't bump your own threads with such comments only a couple of hours after your first post. We're not paid to be your 24/7 support, and we all have other things to do as well. It may well have been early morning in your locale, but those willing/able to help may have just gone to sleep for the night.

I'm assuming that leaving the data in the file is part of the exercise.

Use a std::vector to record the results of tellg() before you read each line. If you then want to go back to that line, then you can just seekg() to that position and re-read the line again.

You'll only need to read the file sequentially when the user asks for a line number greater than the max line you've read so far.

commented: N/A +4

thanks for the advice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.