delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/20/18:45:29

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problem writing in a file...
Date: Wed, 20 May 1998 18:43:13 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 33
Message-ID: <35635C81.4AF@cs.com>
References: <6jv6lo$73e$1 AT news3 DOT isdnet DOT net>
NNTP-Posting-Host: ppp141.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

JCx wrote:
> 
>    To say it clearly:
> 
> It opens system.ini : working
> It looks for [drivers32] : working
> I *should* write a string just after this line : bug

Please don't post source code in encoded binary format; many people here
can't read it.

The problem is that you can't arbitrarily switch between read and write
operations on a single file without calling fflush(), fseek(), or
rewind() inbetween.  Even worse, you are trying to write directly into
an open file, which will have the effect of overwriting whatever is
there.  There's another bug:  your program will loop infinitely if there
is no [drivers32] line.  Finally, fprintf() does not seem to work in the
way you intend; I managed to get the text written into the right place
by using fwrite(), but as I suspected it overwrote the existing text.

The easiest way to handle what you're trying to do is to create a
temporary file and copy the contents of the old file to it, inserting
the appropriate line when you get to that point.  Once the write
operation is confirmed, delete the old file and rename the new one to
the old name.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       |"Men rarely (if ever) manage to dream |
|       aka Fighteer I       |up a god superior to themselves. Most |
|   mailto:fighteer AT cs DOT com   |gods have the manners and morals of a |
| http://www.cs.com/fighteer |spoiled child."    - Lazarus Long     |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019