Writing in the middle of an ascii file??

sjs at gnv.ifas.ufl.edu sjs at gnv.ifas.ufl.edu
Thu Mar 28 08:36:27 AEST 1991


In article <6141 at mahendo.Jpl.Nasa.Gov>, robert at 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 at ifasgnv          | - Silver Lady,               |
 |   /     \   Internet:  sjs at gnv.ifas.ufl.edu |   Intergalactic Touring Band |
 +---------------------------------------------+------------------------------+



More information about the Comp.lang.c mailing list