473,378 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

jump over lines in C

I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

Thanks for any suggestions

Feb 20 '06 #1
9 4294
questions? wrote:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?


If you know the length of the lines then fseek() can be used.
Otherwise, you'll have to scan the file, character by character until
the required number of newline characters are encountered. You can then
fseek() to this position and continue your reading.

Feb 20 '06 #2
questions? wrote:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?


See the recent thread "Re: Read only last line-".
Your problem is essentially the same as the topic of
that thread.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 20 '06 #3
questions? wrote:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?


See also this thread:

http://groups.google.com/group/comp....886b31d629cb82

Feb 20 '06 #4
santosh wrote:
questions? wrote:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

If you know the length of the lines then fseek() can be used.


We've just beaten this to death in another thread, but
it seems some people weren't paying attention. It doesn't
matter whether the line lengths are known or not; either
way, fseek() is *NOT* appropriate for this purpose.
Otherwise, you'll have to scan the file, character by character until
the required number of newline characters are encountered. You can then
fseek() to this position and continue your reading.


Once you've read past the intervening lines, you're already
positioned to read the "target" line and no fseek() is needed.

--
Eric Sosman
es*****@acm-dot-org.invalid

Feb 20 '06 #5

santosh wrote:
questions? wrote:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?


See also this thread:

http://groups.google.com/group/comp....886b31d629cb82

Thanks you guys all for the reply

The line of disscussion is helpful!

Feb 20 '06 #6
questions? a écrit :
I opened a file with

fileptr=fopen(filaneme,"r");
Good. Don't forget to test fileptr against NULL before using it.
I then use

fgets(a_string, sizeof(a_string),fileptr);
Useless parens...

fgets(a_string, sizeof a_string, fileptr);
to read each line.
How to jump over a prespecified number of lines without doing fgets?


You don't. Iterate, count the fgets() and check for the value it returns
(NULL mean end of reading, whatever the reason).

Also check the presence of the '\n' in each read block to be sure it's a
complete line...

You also can read by bytes with fgetc() and count the '\n'. Probably
more a simple way...

--
C is a sharp tool
Feb 20 '06 #7
On 2006-02-20, Eric Sosman <es*****@acm-dot-org.invalid> wrote:
santosh wrote:
questions? wrote:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

If you know the length of the lines then fseek() can be used.


We've just beaten this to death in another thread, but it
seems some people weren't paying attention. It doesn't matter
whether the line lengths are known or not; either way, fseek() is
*NOT* appropriate for this purpose.


Eh - whatever one might propose to do to find and save the lengths
of the lines in advance, you can do to instead save the ftell
positions of them, so it's the same class of problem. I believe this
is done for modern implementations of fortune(6) on unix systems.
Otherwise, you'll have to scan the file, character by character
until the required number of newline characters are encountered.
You can then fseek() to this position and continue your reading.


Once you've read past the intervening lines, you're already
positioned to read the "target" line and no fseek() is needed.

Feb 20 '06 #8
Jordan Abel wrote:
On 2006-02-20, Eric Sosman <es*****@acm-dot-org.invalid> wrote:
santosh wrote:
questions? wrote:
[...]
How to jump over a prespecified number of lines without doing fgets?

If you know the length of the lines then fseek() can be used.


We've just beaten this to death in another thread, but it
seems some people weren't paying attention. It doesn't matter
whether the line lengths are known or not; either way, fseek() is
*NOT* appropriate for this purpose.


Eh - whatever one might propose to do to find and save the lengths
of the lines in advance, you can do to instead save the ftell
positions of them, so it's the same class of problem. I believe this
is done for modern implementations of fortune(6) on unix systems.


Yes, if you've already read or written the file sequentially,
you can arrange to record ftell() values during the sequential pass
and use them again later on as an index that allows direct access
to the recorded positions. Still better would be to use fgetpos(),
because ftell() doesn't record (hence fseek() can't restore) the
shift states for files with multibyte characters.

But this doesn't really satisfy the O.P.'s desire to dispense
with the sequential pass altogether: at least one sequential pass
is still needed to gather the ftell() or fgetpos() data. If given
a file without an accompanying index of recorded positions/states,
straight sequential reading is the best one can (portably) do.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 20 '06 #9
questions? wrote:

I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

Thanks for any suggestions

/*
** If the skip_n_lines function attempts
** to read past the end of the file, then rc will equal EOF.
*/
int skip_n_lines(FILE *fd, long unsigned n)
{
int rc = 0;

while (n-- != 0 && rc != EOF) {
rc = fscanf(fd, "%*[^\n]");
if (!feof(fd)) {
getc(fd);
}
}
return rc;
}

--
pete
Feb 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: cousaert | last post by:
Newsgroups: alt.comp.lang.learn.c-c++ Date: Fri, 27 Aug 2004 12:36:11 +0200 Lines: 36 User-Agent: KNode/0.7.6 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii...
1
by: Mel | last post by:
i am reading html pages from files, once i read and display the contect, i want to go to the end of that page that could be several 100 lines long. i need a java script to execute after i placed...
1
by: Blankdraw | last post by:
I thought I cut out for myself a "simple" 1-module ANSI C programming task. I will in fact require extensive use of step-thru debugging. My old MS Visual C Developer Studio 4.2 should do this,...
9
by: paul c | last post by:
Apologies if I'm sending this to the wrong group. If so I'd be grateful if somebody could point me to the right one. I'm using microsoft visual c++ 6.0 compiler. My code is C, I just use the...
4
by: robert d via AccessMonster.com | last post by:
When my app starts up, it creates a temporary database. This temp database is created from a 'model' database that is in the same folder as the application. Because there is a model, the creation...
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
0
by: ane1717 | last post by:
I am trying to write a code that runs when the user clicks a picture within another excel spreadsheet. I need it jump to a specified cell by searching for the contents within the cell "152Z33XX". I...
3
by: mckbill | last post by:
Is there a way I can direct the cursor to a specific field (variable) in a form by typing the field name while in form view? I have a form with many fields, and it would be nice if there were...
13
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
This is a follow-up to my post "Silverlight video doesn't work when file is streamed from handler in ASP.net" at...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.