Find a string and continue past it

Jun 15, 2011 at 4:06pm
If I encounter the desired string in a file, how can I get the program to basically continue and write the next lines in a new vector?
This is what I did:
1
2
3
4
5
6
7
8
9
10
    if ( pFile != NULL ) {
         while(!feof(pFile)) {
             fgets(szQuestion, 100, pFile);
             if(strstr(szQuestion,"IT:")) {
                ITQuestions.push_back(szQuestion);
                continue;
                }
             BQuestions.push_back(szQuestion);
             }   
    }


The thing is, the only line I get in the ITQuestions vector is IT:
After that, the next questions are written in the BQuestions vector. I know this is basically what I told it to do, find IT:, write it in the ITQuestions vector and then continue what it was doing, but I have no idea how to make this work. Please, help? xD
Thanks in advance,
BreaKer.
Jun 15, 2011 at 4:11pm
Replace "continue" with another loop that reads from the file into your vector. This tells the program to enter that read-to-vector loop after finding the "IT:" string.
Jun 15, 2011 at 4:15pm
The thing is, if I do the loop after the IF statement, it starts from the head of the file, not from the IT: part, so it writes all of it both in the ITQuestions and BQuestions...
Last edited on Jun 15, 2011 at 4:19pm
Jun 15, 2011 at 4:22pm
That won't happen if you use the same stream that you do in the previous read-in loop.

Alternativley you can read the whole file into memory and parse it afterward. This might seem like it would take a while but trust me, it won't. It also makes working with the data ten times easier because at that point it's just data in memory, there's no need to worry about the source file.
Jun 15, 2011 at 4:26pm
So you mean read the whole file in a Vector, then decompose the vector in multiple vectors, right?
The problem is, I'm kinda' new in the business, so I am not really sure how can I do that.
Maybe a strstr to find the IT: and then a for loop to strcpy it to another vector and so on?
I'll give it a try :)
Thanks again for your time!
Jun 15, 2011 at 4:53pm
Considering that the second part it's kinda hard for me, I tried with the read loop as you said and it worked! I can't thank you enough xD
Thanks again for your time,
BreaKer.
Topic archived. No new replies allowed.