Jump to content

Reading a certain line in a file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Suppose I want to read the 50th line of a text file, is there a way to do that?

#2
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
skip through the file until you get to the 49th newline.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#3
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Thank you Aereshaa. But how would I make my program skip to the 49th newline?

I'm newbie with this, so I'm not sure I understand you correctly.

The only line reading I know is fgets, which reads a line. So, if I want to read the 50th line, I would have to put it in a loop and count how many time I calls fgets?

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
That's correct. However, make sure you create a buffer to use fgets on.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#5
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
So paying attention in class does pay off :) Thank you. But what's a buffer? And how can I create that?

Another question, suppose I looked at the 50th line. How do I restart at the first line again?

Edited by sourlemon, 28 February 2009 - 05:05 PM.


#6
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
A buffer means the string that you read the line into:
char buf[100];
To restart a file, call fseek(file, 0, SEEK_SET)
if you have any problems post your code and we'll work them out.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#7
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Ahhh that's a buffer. Got that and the fseek :) Thank you Aereshaa.