Re: how do i read odd numbered lines from a file



"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.
.



Relevant Pages

  • Re: New Records printing on odd pages - Word 2000
    ... So if you convert one section break to an Odd Page one, ... >> What I have is a mail merged document which reads off an access ... >> According to the database, if one field is "1" for instance, then the ... >> into the document from the access database, ...
    (microsoft.public.word.docmanagement)
  • Re: cannot write to sql 2000 database after about 2 hours uptime
    ... is happening to your server i.e. Disk, ... database script, I don't need the table definitions, sp, ... anything odd. ... >Db is not set as 'Auto Close'. ...
    (microsoft.public.sqlserver.server)
  • Re: two way replication
    ... use bi-directional transactional replication and have different seeds on ... each side, even on one, odd on the other. ... US database it should reflect in Ind database and vice-versa. ... I thought Transaction replication..but I am facing problems with PK's.. ...
    (microsoft.public.sqlserver.replication)
  • Re: how do i read odd numbered lines from a file
    ... The right answer is to use a database. ... I was thinking about reading only the odd numbered lines (IOW: ... skipping lines) which can't really be done without some sort of assumptions ...
    (comp.lang.c)
  • Re: Loading a data file containing character fields with different encodings
    ... The data is coming from one database that contains UTF-8 characters and it appears that he's attempting to load ... UTF-8 characters along with Latin-1 characters. ... it would be just as easy to write the loader script that converts the encoding to a "unicode" intermediate format and then load with the correct database encoding. ...
    (comp.databases.informix)