Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how do i read odd numbered lines from a file

Reply
Thread Tools

how do i read odd numbered lines from a file

 
 
rudranee@gmail.com
Guest
Posts: n/a
 
      06-04-2006
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work
Can someone give me the code please.

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      06-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hi there,
> can anyone tell me how to lines from a file which are odd numbered i.e.
> 1st,3rd,5th...lines.
>
> i tried incrementing file pointer by 2 (fp=fp+2)
> but it does'nt work
> Can someone give me the code please.


You could just read in two lines to the same char-array, thereby
discarding the one you don't want. And the filepointer is incremented
the correct number of bytes each time.

So the problem with fp=fp+2 is that line lengths can vary and you don't
know each linelength on beforehand...


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-04-2006
Martin Jørgensen said:

> (E-Mail Removed) wrote:
>> hi there,
>> can anyone tell me how to lines from a file which are odd numbered i.e.
>> 1st,3rd,5th...lines.
>>
>> i tried incrementing file pointer by 2 (fp=fp+2)
>> but it does'nt work
>> Can someone give me the code please.

>
> You could just read in two lines to the same char-array, thereby
> discarding the one you don't want. And the filepointer is incremented
> the correct number of bytes each time.


No, it isn't!

The file pointer is NOT incremented, because the file pointer does not point
to the file! I know it sounds like it does, but it really, really doesn't.
What it points to is some internal doodad that contains information about
the file. One of the items of information that internal doodad stores is a
"file position indicator" - and /that/ is incremented.

> So the problem with fp=fp+2 is that line lengths can vary and you don't
> know each linelength on beforehand...


No, the problem with fp=fp+2 is that it turns a valid pointer into an
invalid one for no gain. Please don't give advice unless you are absolutely
100% sure it's correct - and even then, please check in K&R or the Standard
to ensure that you are right to be sure.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      06-04-2006
(E-Mail Removed) wrote:
>can anyone tell me how to lines from a file which are odd numbered i.e.
>1st,3rd,5th...lines.
>
>i tried incrementing file pointer by 2 (fp=fp+2)
>but it does'nt work
>Can someone give me the code please.


If by "incrementing the file pointer" you mean you did this:

#include <stdio.h>

...
FILE *fp = fopen(...);
...
fp = fp + 2;
...

That does not do what you want; Surprisingly, it increments the file
pointer, it does not read from the file, or modify the current
position in the file stream in any way.
After the increment fp will have a value 2*sizeof(FILE) bytes larger
than before, pointing to a memory area that most likely is not a FILE
structure and definitively is not the FILE structure you opened.
Attempting to read via fp will cause undefined behavior. (Because it
will modify random memory locations)

Hint: If you had in front of you a document (printed single side) and
you wanted to read only the odd numbered pages. How would you do it?
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      06-04-2006
(E-Mail Removed) wrote:
> hi there,
> can anyone tell me how to lines from a file which are odd numbered i.e.
> 1st,3rd,5th...lines.


A line is defined in C as a sequence of characters terminated by a
newline character. While the actual end-of-line sequence varies between
operating systems, the C standard library ensures conversion to the
newline character.

So to read odd numbered lines, you'll have to read from the beginning
of the file and keep track of each line. You can store the odd lines
into your buffer while even lines can simply be read and discarded.

> i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work


A pointer to type FILE is not a "file pointer" in the sense you seem to
understand. It points to an internal, implementation defined, data
structure which hold a lot of "meta" information about the file to
which it is attached. Merely changing this pointer's value will give
you pointer pointing to an unknown area of memory, which will
eventually cause undefined behaviour.

> Can someone give me the code please.


This group helps those who make a sincere attempt, not those who simply
want to pass the course, by hook or by crook, even if it's only for a
mind-numbing BPO job.

So, try your best attempt, and if you encounter problems, post your
question and your code.

 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      06-04-2006
(E-Mail Removed) wrote:
> hi there,
> can anyone tell me how to lines from a file which are odd numbered i.e.
> 1st,3rd,5th...lines.


Others have given you some good advice already. Here is some pseudo-code:

FILE *fp = fopen(filename, "r");
if(fp)
{
char buf[1024];
while(fgets(buf, sizeof buf, fp))
{
/* discard the line we just read and read another */
if(!fgets(buf, sizeof buf, fp)) break;

do_something_with(buf);
}
}

> i tried incrementing file pointer by 2 (fp=fp+2)
> but it does'nt work


Sorry but this just made me laugh out loud!

The file pointer does not point into an array of lines in the file. It
points to some data structure that, in a system-specific way, holds
whatever state information is required about an open file, the current
position, and the buffering. A FILE* object should only ever hold either
a null pointer or the result of an fopen function.

There is a way to seek to a particular location in a file if you know
its byte offset. Unfortunately, there's no simple way to determine where
each line starts and ends, without reading all the characters in and
comparing them to '\n'. That's what the fgets function that I used above
does.

--
Simon.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-04-2006
Simon Biber wrote:
> (E-Mail Removed) wrote:
>
>> can anyone tell me how to lines from a file which are odd
>> numbered i.e. 1st,3rd,5th...lines.

>
> Others have given you some good advice already. Here is some
> pseudo-code:
>
> FILE *fp = fopen(filename, "r");
> if(fp)
> {
> char buf[1024];
> while(fgets(buf, sizeof buf, fp))
> {
> /* discard the line we just read and read another */
> if(!fgets(buf, sizeof buf, fp)) break;
>
> do_something_with(buf);
> }
> }


That has the problem of coping with the EOL actions of fgets. I
suggest ggets (see below for availability) is more suitable.

char *buffer;
FILE *fp;

if (fp = fopen(filename, "r") {
do {
if (0 != fggets(fp, &buffer) break;
free(buffer); /* discard odd numbered lines */
if (0 != fggets(fp, &buffer) break;
doSomethingWith(buffer);
free(buffer);
} while (1);
} /* untested */

See <http://cbfalconer.home.att.net/download/>

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.

 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      06-04-2006
On 2006-06-04, Simon Biber <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>> hi there,
>> can anyone tell me how to lines from a file which are odd numbered i.e.
>> 1st,3rd,5th...lines.

>
> Others have given you some good advice already. Here is some pseudo-code:
>

That's some mighty C-looking psuedo-code.

> FILE *fp = fopen(filename, "r");
> if(fp)
> {
> char buf[1024];
> while(fgets(buf, sizeof buf, fp))
> {
> /* discard the line we just read and read another */
> if(!fgets(buf, sizeof buf, fp)) break;
>
> do_something_with(buf);
> }
> }
>
>> i tried incrementing file pointer by 2 (fp=fp+2)
>> but it does'nt work

>
> Sorry but this just made me laugh out loud!
>

It made me reread the original post a few times. Then I laughed.

From your code I read "sizeof buf", which is one of those obvious things
I never thought of. I would have always written 1024, and then changed
the number in two places every time I needed to. The unnecessary pains
I go through because I don't think...

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
 
Reply With Quote
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      06-04-2006
Richard Heathfield wrote:
> Martin Jørgensen said:
>
>
>>(E-Mail Removed) wrote:
>>
>>>hi there,
>>>can anyone tell me how to lines from a file which are odd numbered i.e.
>>>1st,3rd,5th...lines.
>>>
>>>i tried incrementing file pointer by 2 (fp=fp+2)
>>>but it does'nt work
>>>Can someone give me the code please.

>>
>>You could just read in two lines to the same char-array, thereby
>>discarding the one you don't want. And the filepointer is incremented
>>the correct number of bytes each time.

>
>
> No, it isn't!
>
> The file pointer is NOT incremented, because the file pointer does not point
> to the file! I know it sounds like it does, but it really, really doesn't.
> What it points to is some internal doodad that contains information about
> the file. One of the items of information that internal doodad stores is a
> "file position indicator" - and /that/ is incremented.


I also meant that.

>>So the problem with fp=fp+2 is that line lengths can vary and you don't
>>know each linelength on beforehand...

>
>
> No, the problem with fp=fp+2 is that it turns a valid pointer into an
> invalid one for no gain. Please don't give advice unless you are absolutely
> 100% sure it's correct - and even then, please check in K&R or the Standard
> to ensure that you are right to be sure.


That was my mistake... But please don't think that errors don't get
corrected by other people in this newsgroup and that it therefore is an
absolute catastropy to write comments to other peoples posts. This group
is large enough for both (or all three of us). I was thinking of some
FSEEK code I recently made where the file position can be incremented or
stored just as the OP had in mind and I don't see anything wrong in
telling him that the line lengths vary and therefore he can't do what he
probably thought he could do.


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-04-2006
Simon Biber wrote:
> (E-Mail Removed) wrote:
> > hi there,
> > can anyone tell me how to lines from a file which are odd numbered i.e.
> > 1st,3rd,5th...lines.

>
> Others have given you some good advice already. Here is some pseudo-code:
>
> FILE *fp = fopen(filename, "r");
> if(fp)
> {
> char buf[1024];
> while(fgets(buf, sizeof buf, fp))
> {
> /* discard the line we just read and read another */
> if(!fgets(buf, sizeof buf, fp)) break;
>
> do_something_with(buf);
> }
> }


Note that this processes the 2nd, 4th, etc... lines, and at face value
it ignores
the possibility that a line may be longer than sizeof buf characters.

You can avoid storing the line unnecessarily by doing something like...

if (fscanf(fp, "%*[^\n]") != EOF)
fgetc(fp);

--
Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how do i read odd numbered lines from a file rudranee@gmail.com C Programming 0 06-04-2006 11:11 AM
Problems with numbered lists that use css Dave Rado HTML 13 05-10-2004 11:03 PM
A program to make a numbered list? Metaphor Computer Support 5 02-26-2004 04:44 PM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments