Date: Thu, 11 Dec 1997 12:27:14 +0200 (IST) From: Eli Zaretskii To: Mark Favata cc: djgpp AT delorie DOT com Subject: Re: Error Writing to File In-Reply-To: <348EFB98.25F8@epix.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Wed, 10 Dec 1997, Mark Favata wrote: > 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? > > This is like the code I am using: > > if((in = fopen(argv[1], "rb+")) == NULL) > { > printf("Error opening output file.\n"); > exit(-1); > } > > while(!feof(in)) > { > fprintf(in, "0"); > } feof' doesn't work when you write to a file, because you should be able to expand the file by writing beyond its edge. And that's exactly what happens in your case. If you want `feof' to work, you need to read the file. But reading and writing to the same file is tricky. So I'd suggest to change your program so it just writes the proper number of bytes without reading the file: you can know the size of the file without reading it. Try the (untested) program below. It might be not good enough for very large (say, more than 2MB) files because it allocates a buffer the size of the file, but you could allocate a smaller buffer and overwrite such large files in several writes instead of one. #include #include #include #include #include int main (int argc, char *argv[]) { if (argc > 1) { FILE *fp; struct stat st; off_t fsize; char *junk_buf = NULL; if (stat (argv[1], &st) == -1) { perror (argv[1]); return 1; } fsize = st.st_size; if ((junk_buf = (char *)malloc (fsize)) == NULL) { perror ("memory exhausted"); return 2; } memset (junk_buf, 0, fsize); fwrite (junk_buf, 1, fsize, fp); return fclose (fp); } else { fprintf (stderr, "Usage: %s filename\n", argv[0]); return 0; } }