Re: how do i read odd numbered lines from a file
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 08 Jun 2006 22:04:50 GMT
"Dann Corbit" <dcorbit@xxxxxxxxx> writes:
"Ben Pfaff" <blp@xxxxxxxxxxxxxxx> wrote in message[...]
news:87pshjfn7w.fsf@xxxxxxxxxxxxxxx
"Dann Corbit" <dcorbit@xxxxxxxxx> writes:
rudranee@xxxxxxxxx> wrote in message
news:1149419470.636942.308970@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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) kst-u@xxxxxxx <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.
.
- Follow-Ups:
- Re: how do i read odd numbered lines from a file
- From: Dann Corbit
- Re: how do i read odd numbered lines from a file
- References:
- how do i read odd numbered lines from a file
- From: rudranee
- Re: how do i read odd numbered lines from a file
- From: Dann Corbit
- Re: how do i read odd numbered lines from a file
- From: Ben Pfaff
- Re: how do i read odd numbered lines from a file
- From: Dann Corbit
- how do i read odd numbered lines from a file
- Prev by Date: Re: How Do I License & Protect My Software?
- Next by Date: Re: How do I make my own custom C compiler?
- Previous by thread: Re: how do i read odd numbered lines from a file
- Next by thread: Re: how do i read odd numbered lines from a file
- Index(es):
Relevant Pages
|