468,315 Members | 1,488 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

extra character when writing file

Jim
Hi There,

I'm trying to read a file character by character. When I write the
file out, there is one extra character which shows on the screen as a
solid circle with a small question mark in the middle.

Here is what I have:

infile = fopen("encrypted.txt", "r");
outfile = fopen("plain.txt", "w");

while(!feof(infile)) {
ch = fgetc(infile);
fputc(ch, outfile);
}

fclose(infile);
fclose(outfile);

Can someone please tell me why this character is getting added to the
outfile?
Apr 10 '08 #1
4 3227
Jim said:
Hi There,

I'm trying to read a file character by character. When I write the
file out, there is one extra character
I bet you're using feof (wrongly).
which shows on the screen as a
solid circle with a small question mark in the middle.

Here is what I have:

infile = fopen("encrypted.txt", "r");
outfile = fopen("plain.txt", "w");
What if either (or both) of these files fails to open?
>
while(!feof(infile)) {
ch = fgetc(infile);
fputc(ch, outfile);
}

fclose(infile);
fclose(outfile);

Can someone please tell me why this character is getting added to the
outfile?
feof is reactive, not predictive. It won't yield a non-zero result until
you've actually tried to read past the end of the file.

The fix is to stop using feof (or at least, to stop using it in quite that
way):

#include <stdio.h>

int main(int argc, char **argv)
{
if(argc == 3)
{
FILE *infile = fopen(argv[1], "r");
if(infile != NULL)
{
FILE *outfile = fopen(argv[2], "w");
if(outfile != NULL)
{
int ch;
while((ch = getc(infile)) != EOF)
{
putc(ch, outfile);
}
if(ferror(infile))
{
fprintf(stderr, "error reading %s\n", argv[1]);
}
if(ferror(outfile))
{
fprintf(stderr, "error writing %s\n", argv[2]);
}
if(fclose(outfile) != 0)
{
fprintf(stderr, "error closing %s\n", argv[2]);
}
}
else
{
fprintf(stderr, "error opening %s\n", argv[2]);
}
fclose(infile);
}
else
{
fprintf(stderr, "error opening %s\n", argv[1]);
}
}
else
{
fprintf(stderr, "please specify input and output filenames\n");
}

return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 10 '08 #2
Jim wrote:
Hi There,

I'm trying to read a file character by character. When I write the
file out, there is one extra character which shows on the screen as a
solid circle with a small question mark in the middle.

Here is what I have:

infile = fopen("encrypted.txt", "r");
outfile = fopen("plain.txt", "w");

while(!feof(infile)) {
ch = fgetc(infile);
fputc(ch, outfile);
}

fclose(infile);
fclose(outfile);

Can someone please tell me why this character is getting added to the
outfile?
This is a FAQ...

http://c-faq.com/stdio/feof.html

--
Peter
Apr 10 '08 #3
Jim
while((ch = getc(infile)) != EOF)
{
putc(ch, outfile);
}
Thank you that was very helpful.

I'm hoping you can clarify for me... I'm curious why the extra ()
around the ch = getc(infile). Would that not return a true/false and
if so, why does that not trigger the EOF??

Also: Why are you using getc instead of fgetc??
Apr 10 '08 #4
Jim wrote:
>
I'm trying to read a file character by character. When I write the
file out, there is one extra character which shows on the screen as
a solid circle with a small question mark in the middle.

Here is what I have:

infile = fopen("encrypted.txt", "r");
outfile = fopen("plain.txt", "w");

while(!feof(infile)) {
ch = fgetc(infile);
fputc(ch, outfile);
}

fclose(infile);
fclose(outfile);

Can someone please tell me why this character is getting added to the
outfile?
Yes. You are misusing feof. You are displaying EOF, which isn't a
char. See the cfaq (see listing below). Your code should read:

#include <stdio.h>

int main(void) {
int ch; /* note that this is an int */
FILE infile, outfile; /* You need to declare vars you use */

infile = fopen("encrypted.txt", "r");
if (!infile) {
puts("Can't open 'encryped.txt'");
return 0;
}
outfile = fopen("plain.txt", "w");
if (!outfile) {
puts("Can't open 'plain.txt');
fclose(infile);
return 0;
}
/* files all successfully opened */
while (EOF != (ch = getc(infile))) putc(ch, outfile);
fclose(infile);
fclose(outfile);
return 0;
}

And I haven't made you use stdlib and EXIT_SUCCESS or
EXIT_FAILURE. But everything is checked except the fclose calls
and puts calls (and the putc call).

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/ (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2(C99, txt)
<http://www.dinkumware.com/c99.aspx (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

** Posted from http://www.teranews.com **
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by dev_kh | last post: by
18 posts views Thread by james | last post: by
11 posts views Thread by santosh | last post: by
2 posts views Thread by ricky | last post: by
9 posts views Thread by jraul | last post: by
13 posts views Thread by =?Utf-8?B?YXVsZGg=?= | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.