[comp.lang.c] Writing in the middle of an ascii file??

robert@nereid.jpl.nasa.gov (Robert Angelino) (03/27/91)

Hello Usenet people.

I'm trying to read and then write to an ascii file using C only
and have not had any luck doing it. I don't want to use an
intermediate ascii file to do it unless there's absolutely
no other solution.  Has anyone out there done this??

thanks in advance.


-- 
    -     ------       -                              Robert Angelino
   | |   | ----  \    | |                             ms T-1704L
   | |   | |   \ |    | |                             4800 Oak Grove Drive
   | |   | | --  |    | |                             Pasadena, CA 91109
---| |   | | \__/     | |___                          robert@triton.jpl.nasa.gov
\____|et |_|ropulsion |_____\aboratory                (818) 354-9574

sjs@gnv.ifas.ufl.edu (03/28/91)

In article <6141@mahendo.Jpl.Nasa.Gov>, robert@nereid.jpl.nasa.gov
(Robert Angelino) writes:

> I'm trying to read and then write to an ascii file using C only
> and have not had any luck doing it. I don't want to use an
> intermediate ascii file to do it unless there's absolutely
> no other solution.  Has anyone out there done this??

Pull the file into memory.  You can't plop text into the middle of a
file (ASCII or not) and expect the rest to be shuffled over automatically.
You have to do this yourself.  If you read the file into memory, you'll
have to play with links to the inserted parts, or constantly strcpy
(no fun either way).

On the other hand, if you want to overwrite certain parts, and can make
the ASCII file so that it has fixed-size records, then you can just fseek
to the desired line and overwrite the old stuff with the new stuff.  Just
pad lines with spaces or what-not to make them all the same length.  And
open the file in binary mode.


 +---------------------------------------------+------------------------------+
 |     ~~~     Sid Sachs                       | "Time to wake up!            |
 |   / \ ~~~   at the Mountains of Madness     |    Time to rise!"            |
 |  /  / \                                     |                              |
 | /  /   \    Bitnet:    sjs@ifasgnv          | - Silver Lady,               |
 |   /     \   Internet:  sjs@gnv.ifas.ufl.edu |   Intergalactic Touring Band |
 +---------------------------------------------+------------------------------+