how do i read odd numbered lines from a file

R

Richard Bos

Dann Corbit said:
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
 
D

Dann Corbit

Richard Bos said:
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.
 
R

Richard Bos

Dann Corbit said:
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
 
D

Dann Corbit

Richard Bos said:
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.
 
C

CBFalconer

Dann said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top