473,385 Members | 1,927 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,385 software developers and data experts.

how do i read odd numbered lines from a file

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.

Jun 4 '06 #1
24 6096
ru******@gmail.com 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
Jun 4 '06 #2
Martin Jørgensen said:
ru******@gmail.com 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)
Jun 4 '06 #3
ru******@gmail.com 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?
Jun 4 '06 #4
ru******@gmail.com 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.

Jun 4 '06 #5
ru******@gmail.com 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.
Jun 4 '06 #6
On 2006-06-04, Simon Biber <ne**@ralmin.cc> wrote:
ru******@gmail.com 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!
Jun 4 '06 #7
Simon Biber wrote:
ru******@gmail.com 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.

Jun 4 '06 #8
Richard Heathfield wrote:
Martin Jørgensen said:

ru******@gmail.com 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
Jun 4 '06 #9
Simon Biber wrote:
ru******@gmail.com 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

Jun 4 '06 #10
Martin Jørgensen said:
Richard Heathfield wrote:
Martin Jørgensen said:
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...


Indeed.
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.
Well, it's not an absolute catastrophe, no, but it does increase our
workload if people give incorrect advice, and of course there is always the
danger that the advice might slip through the net and go uncorrected.
This group is large enough for both (or all three of us).
And indeed for many more. Please don't misunderstand me. You are most
welcome in comp.lang.c, not least because you seem very capable of
listening, learning, and asking bright questions. We're counting on you to
become an expert so that we can all retire and leave all the questions for
you to answer. :-)

But please do try to take a little more care, that's all. Let me put it this
way - your answer showed that you don't really understand the whole "file
pointer" idea, right? Now, I would be prepared to bet a carrot, or maybe
even a potato, that you /knew/ you didn't really understand it. Am I right?

It turns out that, by answering a question wrongly, you gained the
opportunity to learn a little about file pointers. So all's well that ends
well. But if you get this sneaky feeling that you don't really know what
you're talking about, it's better not to answer at all than to guess.

Trust me on this. I've been there, done that, got several T-shirts and scars
to prove it. I've learned that guessing how C works is not the best way to
help people. Once, quite a few years ago now, I even made the mistake of
saying "The Standard says that such-and-such is the case", because it
seemed obvious to me that it would say that, so I didn't feel it necessary
to go and look - and about eighteen trillion people all yelled "WHERE does
it say that?" So I went and looked - and looked, and looked, and looked,
and to my astonishment the Standard didn't say that at all! I had to
apologise for making an incorrect claim, and of course I felt a bit of a
fool. Don't make the same mistake.

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
Sure, but it's the file /position/ that is modified, not the file pointer.
It is a significant distinction.
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.


That in itself is quite right, when carefully separated from your "file
pointer" advice. :-)

--
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)
Jun 4 '06 #11
Richard Heathfield wrote:
Martin Jørgensen said: -snip-
But please do try to take a little more care, that's all. Let me put it this
Got it.
way - your answer showed that you don't really understand the whole "file
pointer" idea, right? Now, I would be prepared to bet a carrot, or maybe
even a potato, that you /knew/ you didn't really understand it. Am I right?
No. I actually knew it, because I had seen a couple of times that the
file-pointer seem to point to some kind of data-structure holding some
strange data-values. Where did I see it? In my debugger...

It's no big deal and hard not to see IMO. I've now tried debugging
programs many times in windows, linux and on my macintosh computer with
even more different debuggers (tried at least 5-6) and every time I try
to investigate what FILE *fp points to I do see this strange
data-structure. I have also worked enough with pointers to know that
FILE *(fp+2) just points to a different memory location. I use a lot of
pointers in the program you just helped me with the other day and if I
didn't understood how pointers worked, I probably couldn't have done all
of that.

So I simply wrote the wrong answer, because I was thinking of some place
in my code where I got something like: position = ftell(fp); and that
can be incremented or decremented and I thought that the OP meant that
he could add 2 byte positions to get rid of CRLF to solve his problem
(thereby skipping an empty line).

The point is then: That also works for empty lines, but that's probably
not enough for him.
It turns out that, by answering a question wrongly, you gained the
opportunity to learn a little about file pointers. So all's well that ends
well. But if you get this sneaky feeling that you don't really know what
you're talking about, it's better not to answer at all than to guess.
I just got corrected and that's no big deal for me.
Trust me on this. I've been there, done that, got several T-shirts and scars
to prove it. I've learned that guessing how C works is not the best way to
help people. Once, quite a few years ago now, I even made the mistake of
saying "The Standard says that such-and-such is the case", because it
seemed obvious to me that it would say that, so I didn't feel it necessary
to go and look - and about eighteen trillion people all yelled "WHERE does
it say that?" So I went and looked - and looked, and looked, and looked,
and to my astonishment the Standard didn't say that at all! I had to
apologise for making an incorrect claim, and of course I felt a bit of a
fool. Don't make the same mistake.


Well I understand you and we all make mistakes. But if you never had
made any mistake, you probably wouldn't be where you are today. And you
wouldn't be half as good a programmer as you probably are today.
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

Sure, but it's the file /position/ that is modified, not the file pointer.
It is a significant distinction.


If I didn't knew that I probably couldn't have programmed my
import_data.c file, which you perhaps tried just the other day. It also
uses a lot of pointers to read in data from an input-file and it seeks
for [location]-values and does a lot of validation.
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.

That in itself is quite right, when carefully separated from your "file
pointer" advice. :-)


I get the point, but I don't consider the question the OP asked about to
be too difficult for me. I programmed something very similar to what he
asks about and it works quite good, although the code perhaps is
slightly ugly seen from a a more professional programmers point of view.

If I hadn't programmed this myself, well then I think I wouldn't have
answered. So I really don't think this is a very advanced or difficult
question and I don't feel I learned anything really new by this thread.
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 5 '06 #12
ru******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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.


#include <stdio.h>
char string[32767];
int main(void)
{
int counter = 0;
while (fgets(string, sizeof string, stdin)) {
++counter;
if (counter % 2)
fputs(string, stdout);
}
return 0;
}

Of course, this is totally the wrong answer because it does not do what was
asked. It also reads the even numbered lines but it ignores them.

The right answer is to use a database (really -- I'm not kidding).

IMO-YMMV.
Jun 8 '06 #13
"Dann Corbit" <dc*****@connx.com> writes:
ru******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.


The right answer is to use a database (really -- I'm not kidding).


Well, it depends. If you're writing a filter that processes an
entire file in sequential order, then I don't a database is the
right answer. If you're writing something that does complicated
or randomly ordered processing, then something like Berkeley DB
with the Recno backend might make a lot of sense.
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
Jun 8 '06 #14
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Dann Corbit" <dc*****@connx.com> writes:
ru******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.
The right answer is to use a database (really -- I'm not kidding).


Well, it depends. If you're writing a filter that processes an
entire file in sequential order, then I don't a database is the
right answer. If you're writing something that does complicated
or randomly ordered processing, then something like Berkeley DB
with the Recno backend might make a lot of sense.


I would argue that it is not possible in C (without using some sort of
database) to skip over lines in a file in the general case. If that is an
actual requirement, then C needs some kind of a helper (like a database).
Or we can limit the problem to binary files with fixed record length (which
again is not solving the problem as stated).

It's a stupid homework problem anyway. I don't think that the professor
should ask the students to do something that really is not possible to do
correctly with the tool set given.

The problem statement may not have been what was presented though. I am
guessing that we are missing some important details. --
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.

Jun 8 '06 #15
"Dann Corbit" <dc*****@connx.com> writes:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Dann Corbit" <dc*****@connx.com> writes:
ru******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.

The right answer is to use a database (really -- I'm not kidding).


Well, it depends. If you're writing a filter that processes an
entire file in sequential order, then I don't a database is the
right answer.


I would argue that it is not possible in C (without using some sort of
database) to skip over lines in a file in the general case.


The OP didn't say he needed to skip lines. He just said he
needed to read the odd-numbered lines. I don't think there's
anything wrong with reading the whole file to solve that
problem.

[much snippage above, trying to focus on the bits I find
interesting]
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Jun 8 '06 #16
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Dann Corbit" <dc*****@connx.com> writes:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Dann Corbit" <dc*****@connx.com> writes:

ru******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
> hi there,
> can anyone tell me how to lines from a file which are odd numbered
> i.e.
> 1st,3rd,5th...lines.

The right answer is to use a database (really -- I'm not kidding).

Well, it depends. If you're writing a filter that processes an
entire file in sequential order, then I don't a database is the
right answer.
I would argue that it is not possible in C (without using some sort of
database) to skip over lines in a file in the general case.


The OP didn't say he needed to skip lines. He just said he
needed to read the odd-numbered lines. I don't think there's
anything wrong with reading the whole file to solve that
problem.


Quite right. I was thinking about reading only the odd numbered lines (IOW:
skipping lines) which can't really be done without some sort of assumptions
or extensions.

Probably, your answer is a lot better than mine then.
[much snippage above, trying to focus on the bits I find
interesting]
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman

Jun 8 '06 #17
"Dann Corbit" <dc*****@connx.com> writes:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Dann Corbit" <dc*****@connx.com> writes:
ru******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.

The right answer is to use a database (really -- I'm not kidding).


Well, it depends. If you're writing a filter that processes an
entire file in sequential order, then I don't a database is the
right answer. If you're writing something that does complicated
or randomly ordered processing, then something like Berkeley DB
with the Recno backend might make a lot of sense.


I would argue that it is not possible in C (without using some sort of
database) to skip over lines in a file in the general case. If that is an
actual requirement, then C needs some kind of a helper (like a database).
Or we can limit the problem to binary files with fixed record length (which
again is not solving the problem as stated).

It's a stupid homework problem anyway. I don't think that the professor
should ask the students to do something that really is not possible to do
correctly with the tool set given.

[...]

I must be missing something.

First off, the original question was incorrectly stated:

"can anyone tell me how to lines from a file which are odd
numbered ..."

Presumably there should be a verb between "to" and "lines".

Second, for any reasonable interpretation of the original question, I
don't seen any problem with doing it in standard C. Reading a line
whose maximum length isn't known in advance can be tricky, but there
are a number of functions floating around that do exactly that -- and
if you can assume a maximum length, you can just use fgets(). If you
want to store all the odd-numbered lines in memory, it's just a matter
of dynamic allocation: set up a pointer to (the first element of) an
array of char* pointers, each of which points to a dynamically
allocated string, and expand the array using realloc() as necessary.
Skipping every other line as you read the file is trivial. If all you
want to do is filter stdin to stdout, skipping even-numbered lines,
you don't even need to read a line at a time; just keep track whether
you've seen an even or odd number of '\n' characters, and use that to
decide whether to print each character.

Did you see something in the problem statement that I missed?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 8 '06 #18
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
{snip}
Did you see something in the problem statement that I missed?
I misread the problem statement as "read only the odd numbered lines from a
file". I couldn't parse the original problem statement very well, so I read
between the lines a bit.

I think I interpreted it that way, because he was trying to accomplish the
task by advance the file pointer.

Ben was right. Just read the lines and spit out the odd numbered ones (like
the filter program I pasted in my earliest post on the topic). --
Keith Thompson (The_Other_Keith) ks***@mib.org
<http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Jun 8 '06 #19
"Dann Corbit" <dc*****@connx.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
{snip}
Did you see something in the problem statement that I missed?
I misread the problem statement as "read only the odd numbered lines from a
file". I couldn't parse the original problem statement very well, so I read
between the lines a bit.


Which, given the problem statement, is of course exactly what you need
to do. 8-)}
I think I interpreted it that way, because he was trying to accomplish the
task by advance the file pointer.


Ok, I think I see what you mean. You thought he wanted to read only
the odd-numbered lines, not reading the even-numbered lines at all *as
opposed to* reading all the lines and discarding the even numbered
ones. (Which is probably a silly thing to do, even if you can figure
out a way to do it.) It's easy enough if you're allowed to do an
initial scan over the input file and create an index.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 8 '06 #20
"Dann Corbit" <dc*****@connx.com> wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Dann Corbit" <dc*****@connx.com> writes:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Dann Corbit" <dc*****@connx.com> writes:

> ru******@gmail.com> wrote in message
> news:11**********************@g10g2000cwb.googlegr oups.com...
>> hi there,
>> can anyone tell me how to lines from a file which are odd numbered
>> i.e.
>> 1st,3rd,5th...lines.
>
> The right answer is to use a database (really -- I'm not kidding).

Well, it depends. If you're writing a filter that processes an
entire file in sequential order, then I don't a database is the
right answer.

I would argue that it is not possible in C (without using some sort of
database) to skip over lines in a file in the general case.


The OP didn't say he needed to skip lines. He just said he
needed to read the odd-numbered lines. I don't think there's
anything wrong with reading the whole file to solve that
problem.


Quite right. I was thinking about reading only the odd numbered lines (IOW:
skipping lines) which can't really be done without some sort of assumptions
or extensions.


It can't be done with a database, either, because in that case it will
be the database that has to read the entire file to extract the odd
lines. Ok, so your C code won't, but it's cheating.

Richard
Jun 9 '06 #21
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:44***************@news.xs4all.nl...
"Dann Corbit" <dc*****@connx.com> wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
> "Dann Corbit" <dc*****@connx.com> writes:
>
>> "Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
>> news:87************@benpfaff.org...
>>> "Dann Corbit" <dc*****@connx.com> writes:
>>>
>>>> ru******@gmail.com> wrote in message
>>>> news:11**********************@g10g2000cwb.googlegr oups.com...
>>>>> hi there,
>>>>> can anyone tell me how to lines from a file which are odd numbered
>>>>> i.e.
>>>>> 1st,3rd,5th...lines.
>>>>
>>>> The right answer is to use a database (really -- I'm not kidding).
>>>
>>> Well, it depends. If you're writing a filter that processes an
>>> entire file in sequential order, then I don't a database is the
>>> right answer.
>>
>> I would argue that it is not possible in C (without using some sort of
>> database) to skip over lines in a file in the general case.
>
> The OP didn't say he needed to skip lines. He just said he
> needed to read the odd-numbered lines. I don't think there's
> anything wrong with reading the whole file to solve that
> problem.
Quite right. I was thinking about reading only the odd numbered lines
(IOW:
skipping lines) which can't really be done without some sort of
assumptions
or extensions.


It can't be done with a database, either, because in that case it will
be the database that has to read the entire file to extract the odd
lines. Ok, so your C code won't, but it's cheating.


If a row is exactly one page, or if you cluster on (line number) %2, it will
not read the even numbered lines at all.

A database will only do a table scan if that is the cheapest way to examine
the data.
Richard

Jun 9 '06 #22
"Dann Corbit" <dc*****@connx.com> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:44***************@news.xs4all.nl...
"Dann Corbit" <dc*****@connx.com> wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
> "Dann Corbit" <dc*****@connx.com> writes:
>
>> "Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
>> news:87************@benpfaff.org...
>>> "Dann Corbit" <dc*****@connx.com> writes:
>>>
>>>> ru******@gmail.com> wrote in message
>>>> news:11**********************@g10g2000cwb.googlegr oups.com...
>>>>> can anyone tell me how to lines from a file which are odd numbered
>>>>> i.e. 1st,3rd,5th...lines.
>>>>
>>>> The right answer is to use a database (really -- I'm not kidding).
>>>
>>> Well, it depends. If you're writing a filter that processes an
>>> entire file in sequential order, then I don't a database is the
>>> right answer.
>>
>> I would argue that it is not possible in C (without using some sort of
>> database) to skip over lines in a file in the general case.
>
> The OP didn't say he needed to skip lines. He just said he
> needed to read the odd-numbered lines. I don't think there's
> anything wrong with reading the whole file to solve that
> problem.

Quite right. I was thinking about reading only the odd numbered lines (IOW:
skipping lines) which can't really be done without some sort of assumptions
or extensions.


It can't be done with a database, either, because in that case it will
be the database that has to read the entire file to extract the odd
lines. Ok, so your C code won't, but it's cheating.


If a row is exactly one page, or if you cluster on (line number) %2, it will
not read the even numbered lines at all.


If the database can do that, so can the C program. Sure, it won't be ISO
C without any dependencies, but then, neither is code that relies on a
database.
Of course, if you want to do this repeatedly to the same file, a
database is always the way to go.

Richard
Jun 9 '06 #23
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
[snip]
If the database can do that, so can the C program. Sure, it won't be ISO
C without any dependencies, but then, neither is code that relies on a
database.
Of course, if you want to do this repeatedly to the same file, a
database is always the way to go.
One could argue that the database was probably written in C. Therefore, C
can do the job. But lots of effort is required.
Richard


Jun 9 '06 #24
Dann Corbit wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
"Dann Corbit" <dc*****@connx.com> writes:
.... snip ...

I would argue that it is not possible in C (without using some
sort of database) to skip over lines in a file in the general case.


The OP didn't say he needed to skip lines. He just said he
needed to read the odd-numbered lines. I don't think there's
anything wrong with reading the whole file to solve that
problem.


Quite right. I was thinking about reading only the odd numbered
lines (IOW: skipping lines) which can't really be done without
some sort of assumptions or extensions.


You can't advance to the next line in a C text file without reading
the line and scanning for a '\n'. So where do databases come into
it? A solution might be (using stdin/stdout):

#include <stdio.h>
int main(void) {
unsigned int lines;
int ch;

lines = 0;
while (EOF != (ch = getchar())) {
if (lines & 1) putchar(ch);
if ('\n' == ch) lines++;
}
return 0;
}

If you really want even numbered lines you can just alter the
initialization of lines. It depends if you consider the first line
the zeroth or the first :-)

--
"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.
Jun 9 '06 #25

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

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
2
by: VM | last post by:
Is it possible to open an ascii file, read the first 499 lines, overwrite line 500 with the new string, and close the file? All the lines before and after line 500 will be untouched. Thanks.
0
by: rudranee | last post by:
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...
3
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
5
by: jgentes | last post by:
I really like the numbered code lines on this forum. I would like to incorporate something like this on my own site, and was wondering how exactly it was done? I am assuming that its creating some...
6
by: bmerlover | last post by:
I'm trying to read each line, character by character, to determine if a line has a semicolon in it. I'm trying to count the number of lines that have a semicolon. In all of the files in one...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.