Skipping left over lines in a text file

say i have the following text in a file:
.
2546 S d5e3 Bill
5151 C d5e3 Bill

what I want to do is check the first item, if its true i know what to do already. What I cant figure out is how to skip the remainder of the row if I get a false statement.

consider the first column an account number. if its true i will process the information in the row. if its false i want to continue and check the next number. do this until i reach the end of the file.

thanks.
The simple solution is to put the actual check into a function.
Pseudo-Code:
1
2
3
4
5
6
7
8
9
10
11
12
bool Function(class* Arg)
{
    if(Arg->Member != /*What Ever Value*/)
    {
         return false;
     }
     else
     {
       /*code code code*/
      }
return true;
}

By passing the object in by reference, whatever changes you make to it are reflected on the origional source.
Last edited on
If you're iterating over and over through the file, there's no way to just "skip" characters to the next line. All you can really do is ignore them. I'm not sure how you're iterating through your text, but if you're just using a repeated getchar than you could just continue grabbing the next character until you find a newline character ('\n'). You could do something like this:
1
2
for(char i = 0; (i = getchar()) != '\n'; )
    ;
Thanks.

NOT ABLE TO DO THAT... thats what I was afraid of.

Im using
inf >> int;
inf >> char;
inf >> string;
inf >> string;

If I loop this sequence im guessing it will move onto the next line anyway, right?

You can move to the next line using inf.ignore(std::numeric_limits<streamsize>::max(), '\n');. However it would probably be much simpler to just loop on your sequence of reads that should consume the whole line anyway:
1
2
3
4
5
6
7
8
9
10
11
12
13
int acc;
char c;
std::string str1;
std::string str2;

while(inf >> acc >> c >> str1 >> str2)
{
    // all items read successfully
    if(acc == check_acc)
    {
        // ... deal with account details c, str1 & str2
    }
}

Topic archived. No new replies allowed.