Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reposition file pointer to next line?

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
US
If your going through a file line by line and need to suddenly jump to the start of the next line what is a good method to move the FILE pointer to the that next line?

Thanks,
-bitwise
 
huh? seems like u answered ur own question? I must not understand the question.

-pete
 
man fseek().
ex:
Code:
  char buf[100];
  FILE *fd;

     fd = fopen(myfile,"r");
     
      if (fd) {
          while ( (fgets(buf,100,fd)) != NULL) {
                if ( (strcmp(buf,"Foo")) == 0) {
                      fseek(fd,100,SEEK_CUR);
                }
           }
       }
  fclose(fd);
  return 0;
}
 
Your method marsd jumps ahead a random 100 positions. I don't want to do that.

I'll just fgets() the remaining part of the sentence and continue; the loop.

Thanks,
-bitwise
 
Sure, but the idea is the same.
When your condition is met read till a '\n' and
jump 'n' places to the beginning of the next line
as you say.
 
hi bitwise

try the syntax cin.getline(....) this should solve your problem.

this is part of TC++ syntax, and should work with other c++ compilers


icici
 
icici getline() is the same as fgets() just that fgets() is a C function not a C++ function. The program is in C. marsd I should have made the fact more clear, but I'm reading in each word one at a time, not an entire line. Any given word in that sentence may invoke the action to jump to the next line. I can't assume anything about the length of the line. It may have 1 word left in it or it may have 100 words left in it all of varrying sizes. I wanted a simple method that could just reposition to the next line but I guess I'll have to just fgets() the remaining part of the sentence and continue; the loop.

Thanks,
-bitwise
 
Yes, that sounds a bit more complicated.
I don't see how to simplify it given your
criteria.
Good Luck
 
>> I wanted a simple method that could just reposition to
>> the next line

if u want one and it doesn't already exist then u write it ur self... that is why ur calling ur self a programmer yes?

is this a school assignment so you have to use fgets()? if not then perhaps there is a different design altogether that would be more suitable? -There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
Thanks palbano, why didn't I think of that! I'm such an idiot.

-bitwise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top