0

I was wondering if this can be accomplished using the ifstream library and using only a text file. Basically if I have multiple lines of information, regardless of whether or not they're the same length, would I be able to make my program read a specific line?

For example in the following file:

Bob Professor
Greg Singer Microphone Plane
Ed Runner Sun

If I wanted to read specifically line 2, aka the line starting with "Greg", is there a way to use markers to navigate to that line?

For example, is it possible to get to that line using the fact that it's the only one that starts with "Greg"? Or is there some kind of marker that I can place at some point in the lines to identify it? The desired result would be that so if I wanted to pull up a specific user's information from the text file (in this case, Greg), I'd be able to have the program read the information from that line.

Same thing if for example I wanted to pull up Ed, I'd just want it to read the line with Ed's info.

Any help would be greatly appreciated! Keep in mind, I can only use text files, so using another format is out of the question.

BaloneyOs
  • 317
  • 3
  • 10
  • 2
    Yes it is. What have you tried? – OMGtechy Dec 06 '15 at 11:26
  • have you tried readline() function ? what have you done so far? you can read file and parse it in memory too. – alirakiyan Dec 06 '15 at 11:28
  • I haven't tried implementing it yet because I'm not sure where to begin. It's a fundamental roadblock in my project at the moment. I'm a beginner at programming (this is my first semester), so please bear with me if I'm lacking in knowledge =) – BaloneyOs Dec 06 '15 at 11:31
  • *"Or is there some kind of marker that I can place at some point in the lines to identify it?"* Do you mean you can change the text file? This seems odd in combination with *"I can only use text files, so using another format is out of the question"* because a straightforward way of placing markers is to add line numbers at the beginning of each line, but this changes the file format in some way. – leemes Dec 06 '15 at 11:32
  • You need these:std::fstream, std::string, std::readline. And this: http://stackoverflow.com/questions/8095088/how-to-check-string-start-in-c – erenon Dec 06 '15 at 11:33
  • My searches for C++ readline have only turned up getline related results. Is that what it is? – BaloneyOs Dec 06 '15 at 11:49
  • In general if you want to find the line containing some marker you need to read every line looking for the marker. Unless you have some information external to the file telling you where to look there isn't any shortcut. – Alan Stokes Dec 06 '15 at 13:17
  • You need to clarify what you want to do. If you need to read the second line, just read two lines and ignore the first. If you need to find a line that includes "Greg" then you need to read a line at a time and check each line to see if it includes "Greg". If you've already found a line that you're interested in you want to be able to get back to it easily you can save its offset from the beginning of the file. – Pete Becker Dec 06 '15 at 13:23

0 Answers0