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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

move file pointer

Status
Not open for further replies.

monion03

Programmer
Apr 24, 2007
7
0
0
GR
Hello,

We have a problem. We would like to set the pointer of a file in c. We read about fseek but this functions moves the file pointer according to a number of bytes. Unfortunately we don't know the exact byte of the desired pointer position.

Actually, we have a file with just one integer per line and we want to read a specific number of rows of this file.

How can we move the pointer, let's say at line 3?

Thank you in advance.
 
Unless you've already read the file once, you can't seek to a particular line of a file.

For example, to build an index of where all the lines are
Code:
while ( !feof(fp) ) {
  pos[line++] = ftell(fp);
  fgets( buff, sizeof buff, fp );
}

Later on, you can then do something like
Code:
fseek( fp, pos[3], SEEK_SET );
fgets( buff, sizeof buff, fp );


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
If your file has a fixed-length record size (i.e. line size) then you could do what you need to do without having to read every line in the file; otherwise, you'll have to use Salem's method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top