Re: Newbie howto question.



On Tue, 26 Apr 2005 00:19:00 -0000, Longfellow <not@xxxxxxxxxxxx>
wrote:

>I'm just learning how to program, so my question is probably elementary.
>Apologies where appropriate.
>
>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. Each html filename is $GEDCOM_ID
>with the '.html' suffix.
>
>So far, I can open the GEDCOM file and identify a record, and then open
>the related html file. I can identify the tagged information, extract
>said info and write it to the html file, closing the html file before
>going to the next record.
>
>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. This needs two files, so you will need to do a
bit of work with the file system:

BEGIN
open original file for reading
create a new temporary file for writing
read the first line from original file.
WHILE (not end of original file)
IF (line needs changing)
change the line as needed
write changed line to temporary file
ELSE
write original line to temporary file
ENDIF
read next line from original file
ENDWHILE
delete or rename original file
rename temporary file to the old name of the original file
END

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

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

>
>I considered asking in comp.lang.c, but I get the feeling my question
>isn't appropriate there. Read this NG charter, decided this was an
>algorithm matter, so posted here.
>
>All comments and replies appreciated.
>
>Thanks,
>
>Longfellow


The ultimate truth is that there is no ultimate truth
.



Relevant Pages

  • RE: Locked for editing???
    ... removable disk, such as a floppy diskette, the disk has been removed. ... file name and places this file in the same folder as the original file. ... The process was interrupted while creating the temporary file because you ... If the interruption was caused ...
    (microsoft.public.excel.setup)
  • Re: Serialized r/w access to a collection from multiple processes - DLL or EXE?
    ... one or more instances of our app EXEs will be accessing the same ... Flush memory content into new temporary file. ... If write succeeds, then rename original file. ...
    (microsoft.public.vc.language)
  • RE: How is the ~$... file created when you open a document used?
    ... This temporary file holds the logon name of the person ... If the Owner File is damaged or missing the error message changes to: ... Word automatically deletes the Owner File when the original file is closed. ... I understand that Word opens a "copy" of the file and you ...
    (microsoft.public.word.docmanagement)
  • Re: c++ fstream writing to existing file
    ... rewind the original file. ... copy the contents from the temporary file back to the original. ... The universe is a figment of its own imagination. ...
    (comp.lang.cpp)