Re: Newbie howto question.




In article <aret61d4pj80qdtr3o65ateh11k3r1f87h@xxxxxxx>, rossum <rossum48@xxxxxxxxxxxx> writes:
> On Tue, 26 Apr 2005 00:19:00 -0000, Longfellow <not@xxxxxxxxxxxx>
> wrote:
>
>> I've got an ASCII text file (a GEDCOM file) comprised of many records of
>> tagged information. I've used an application to create a webpage for
>> each individual record, but now I want to add further information from
>> each record to the related web page....
>>
>> What I'm trying to figure out is an elegant way of inserting the
>> information into the correct place in the html file. All I can think to
>> do is read each html file into a buffer, testing for the relevant line,
>> then replace it with the revised line, and write the buffer back out
>> again, overwriting the file that was read in. What easier approach am I
>> missing?
>
> You don't have to read the whole file in at once. You can process it
> one line at a time, either copying the line unchanged or replacing it
> with an amended line.

More generally, file editing can be done with a buffer of any size
(provided you can store enough state to know what you're currently
looking at), from a bit to the entire file. Sometimes it's most
convenient to simply buffer the entire file, operate on the buffer,
and write it back out; often working with smaller pieces (and lines
are a typical choice for text files) is more suitable, and that
approach is often more space-efficient and scalable as well.

And despite what Phlip says, this is an easy task to accomplish in
C. It's moderately easier in higher-level languages, and if I were
doing it (again) I might use one, depending on other constraints;
but C will serve just fine, particularly since you can impose
restrictions on your input, since you know what it is. That means,
for example, that you can use a fixed-size line buffer and not worry
about over-long lines.

On the other hand, if you're just learning programming, learning a
higher-level language like Ruby or Python (Perl is also suitable, but
I don't recommend it, as its syntax is abominable and it suffers from
a surfeit of overlapping features) could be a useful educational
experience.

>> I'm using ANSI C and wish to avoid any non-standard usages.

It's impossible to write a non-trivial C program that doesn't depend
in some way on implementation-defined behavior, but you can accomplish
this task using only functions from the standard library, so you're in
good shape.

> remove(), rename() and tmpnam() are all in C99, I am not sure about
> ANSI C.

C99 is "ANSI C" - it's the most recent version of the ISO C standard,
which was adopted by ANSI. But remove, rename, and tmpnam have all
been part of the standard C library since the first ANSI standard.

>> I considered asking in comp.lang.c, but I get the feeling my question
>> isn't appropriate there.

I think it's on-topic for comp.lang.c, since it asks, in effect,
"what's a good way to accomplish this task using only the standard
features of the language?". However, you'd get the same answers you
got here.

--
Michael Wojcik michael.wojcik@xxxxxxxxxxxxxx

Pocket #16: A Ventriloquist's "Helper" -- Recordings for Divers Occasions,
especially cries to put in the mouths of enemies -- "God Bless Captain
Vere!" "Les jeux sont faits!" &c. -- Joe Green
.



Relevant Pages

  • Re: newbe: documentation string length
    ... as it addresses a reasonable goal state for a standard. ... with the Iraq war in that it's popular for the Bush camp over the last ... it hoping to make its meaning as apparent as possible, ... > The moral I'm minded to draw is that the ANSI ...
    (comp.lang.lisp)
  • Re: Great SWT Program
    ... And the emacs term "buffer" is not identical to this. ... Emacs generally doesn't care about your own standard. ... launching "flashlight" is still carrying a concealed gun. ...
    (comp.lang.java.programmer)
  • Re: ANSI C compliance
    ... because I'm liable to be quoted out of context -- ANSI ... Several others have made this point, but even when portability is ... or for a million other but-what-difference-can-that-make reasons. ... Standard conformance is an instance of undefined behavior, ...
    (comp.lang.c)
  • ANSI C compliance
    ... "My boss would fire me if I wrote 100% ANSI C code" ... standard which is defined by an international committee. ... as intended on all platforms for which you have an ANSI C compiler, ... Writing truly standard C as valued by the "regulars" ...
    (comp.lang.c)
  • Re: Buffer overflows and asctime()
    ... to contain a "bug" as others are not. ... standard the year member receives a maximum value. ... will overflow its buffer if confronted with valid inputs. ... So you still do not understand "undefined behavior". ...
    (comp.std.c)