Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: extra character when writing file

Reply
Thread Tools

Re: extra character when writing file

 
 
Bartc
Guest
Posts: n/a
 
      04-10-2008

"Jim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)) {


That's the wrong way to use feof() as has been explained.

I found it helpful myself to think of feof() as being named feof_triggered()
instead. So it's not true until an attempt is made to read past EOF.

I also made my own feof() which works the way you and I might expect, but
that involves a lot of bad code using fseeks, ftells and assorted
assumptions, so I won't post it here!

> ch = fgetc(infile);


ch returns an EOF value (usually -1) at the end of file. That's what you
should be looking for. I revised your loop to:

while(1)
{ ch=fgetc(infile);
if (ch==EOF) break;
fputc(ch, outfile);
};

--
Bart


 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      04-10-2008
Bartc wrote:
) I also made my own feof() which works the way you and I might expect, but
) that involves a lot of bad code using fseeks, ftells and assorted
) assumptions, so I won't post it here!

What's wrong with using getc() and ungetc() ?

int my_feof(FILE *stream) {
int c = getc(stream);
if (c == EOF) {
if feof(stream) return F_EOF;
if ferror(stream) return F_ERROR;
return F_UNKNOWN;
}
if (ungetc(c, stream) == EOF) return F_UNKNOWN;
return F_OK;
}

Which, I think, should work just dandy.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
 
Bartc
Guest
Posts: n/a
 
      04-10-2008

"Willem" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bartc wrote:
> ) I also made my own feof() which works the way you and I might expect,
> but
> ) that involves a lot of bad code using fseeks, ftells and assorted
> ) assumptions, so I won't post it here!
>
> What's wrong with using getc() and ungetc() ?


Well, I never thought of using ungetc(). I've just tried a simplified
version of your code as:

/* Return 1 (true) if positioned at EOF, or on error */
/* Return 0 (false) otherwise */

int pascal_eof(FILE *stream) {
int c = getc(stream);
if (c == EOF) return 1;
if (ungetc(c,stream) == EOF) return 1;
return 0;
}

--
Bart


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does return-by-value mean extra copies and extra overhead? mathieu C++ 3 09-04-2009 04:25 PM
Scanning a file character by character Spacebar265 Python 18 02-22-2009 03:21 PM
Weird behavior: fputc adds an extra character. Shafik C Programming 2 02-06-2007 07:27 PM
newbie-Why does an extra character appear? Bob C Programming 3 11-28-2005 03:17 AM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM



Advertisments