UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2009-04-14 07:58 AM

Jana
Member
Registered: 2008-03-24
Posts: 21

Re: How to skip some lines in C file reading

HI,
In a file after finding a perticular heading(pattern), I have to skip 3 or 4 lines and then starts reading line by line.

How to skip lines in C file reading ?? Because fgets() reads lines sequentially.

To skip 3 lines, should I call fgets() 3 times without using return values ? is there any alternative way to skip 'n' lines in a file ?


Thanks

Offline

#2 2009-04-14 11:40 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: How to skip some lines in C file reading

Yes, something like that. That's the problem of lines, you don't know how long
they are, so you need to search for three '\n' chars one way or the other.

One thing to look out for with fgets() is how it handles lines longer than your
bufffer, if they're too long fgets() will return multiple times for one line.

Offline

#3 2009-04-14 01:12 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: How to skip some lines in C file reading

When you need to deal with lines of arbitrary length, may I suggest using getline(), if
you're using glibc...  That will dynamically allocate and resize your input buffer as large
as it needs to be to hold each line...

Offline

Board footer

Powered by FluxBB