www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/09/08:06:13

Message-Id: <1.5.4.32.19970909120624.006a2f28@dce03.ipt.br>
Mime-Version: 1.0
Date: Tue, 09 Sep 1997 09:06:24 -0300
To: Nate Eldredge <eldredge AT ap DOT net>, cbalciza AT mail DOT cccis DOT ro,
djgpp AT delorie DOT com (djgpp mailing list)
From: Cesar Scarpini Rabak <csrabak AT dce03 DOT ipt DOT br>
Subject: Re: file I/O (R/W) problem

At 21:30 08/09/97 -0700, Nate Eldredge wrote:
>At 10:48  9/8/1997 GMT, cbalciza AT mail DOT cccis DOT ro wrote:
>>how can I open a file for both reading and writing without
>>erasing the previous file content ?
>I don't think rewriting a file in place works. The preferred method is to
>write the new version to a temporary file, then replace the original with
>it. A good example is the `utod' and `dtou' programs that come with DJGPP;
>find their source in the library source zip.
>

Nate,

The original mail still did not arrived at my account, so the comment is
only about the snippet quoted.

I'm affraid that what the original poster intends to do is feasible with a
call to fopen with the "r+" flag.

If the kind of writting intended to the existing file resumes only to adding
records to the end of the file (like a log file or similar chore) then a
safer alternative would be to open the file with the "a" flag. This flag is
usually the best of both worlds: if the file still does not exist it is
created, else it will only accept writes to _the end_ of it, but will allow
reads to any position  (requires fseeking it of course).

Although your advising is wise from a safety point of view, writing to an
already existing file is a common task in programming.

HTH,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cesar Scarpini Rabak                      E-mail: csrabak AT ipt DOT br
DME/ASC                                   Phone: 55-11-268-3522 Ext.350
IPT - Instituto de Pesquisas Tecnologicas Fax:   55-11-268-5996
Av. Prof. Almeida Prado, 532.  Sao Paulo - SP 05508-901 BRAZIL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Raw text -


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