delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/11/18:48:27

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Error Writing to File
Date: Wed, 10 Dec 1997 20:14:24 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 39
Message-ID: <348EF820.4FFF@cs.com>
References: <348EFB98 DOT 25F8 AT epix DOT net>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp225.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mark Favata wrote:
> 
> Can someone please help me.  I am writing an encryption program and
> want to add a file wipe feature.  I plan on overwriting the file with 1,
> then 2, etc.  The problem is, it keeps writing past the eof mark.
> Within about 2 seconds it has already written 2 megs to the file.  My
> code doesn't seem to stop for the EOF.  Can you tell me what is wrong
> with my code?
> 
> while(!feof(in))
> {
>  fprintf(in, "0");
> }

The EOF condition is only triggered when reading from a file, not when
writing to it.  By writing to the file, you are constantly increasing
its size, rendering the test for EOF null in any case.  The only
portable way to overwrite a file of unknown size without creating a
temporary file to hold the data is to read its size _first_, then
overwrite to exactly that size.  The DJGPP library provides a
(nonportable) function named filelength() to detect the size of a file
opened for reading.  A more portable way would be to read the file in,
count the bytes, then close the file, reopen for writing, and write
exactly that many bytes.  If the file is open in append mode, there are
certain protocols you must use in switching between read and write
operations.

You seem to be fairly mixed up about how the various file opening modes
work in C programs.  I suggest you look through the DJGPP libc
documentation for fopen() and related functions to see how they handle
the various modes.  You may also want to consult your C textbook.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | A singer in a smoky room / Smell of  |
|       aka Fighteer I       | wine and cheap perfume / For a smile |
|   mailto:fighteer AT cs DOT com   | they can share the night / It goes   |
| http://www.cs.com/fighteer | on and on and on...                  |
---------------------------------------------------------------------

- Raw text -


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