472,958 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 3583
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: dev_kh | last post by:
Hi, I am facing a weird issue. Here it goes: I am writing a c# app for a client with which I will read some data and generate a .dat file for them from that data. Then the client will read...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
2
by: ricky | last post by:
Can anybody help with the function to get rid of extra characters in the file. I want to remove the string from the file.So i read from input file and pass the string say "john" if found dnt write...
1
by: asenthil | last post by:
Hai, i had tried to write a string which is fetched from a database into a file.... Here are my codings to write that fetched string into a text file... HANDLE hFile; DWORD wmWritten;...
2
by: Shafik | last post by:
Hello folks, I am having a really weird problem. Under windows XP, a simple C program designed to write to a file has been adding EXTRA characters to the output file. First I used "fwrite",...
9
by: sekarm | last post by:
Hi there, Hi Everybody, I am facing one problem to validate the xml file. first i load the xml file through XmlDocumentType Class. Before load the Xml file i add the code xmlresolver =...
9
by: jraul | last post by:
1) Am I correct that C++ does not have a defined character set? In particular, a platform might not use the ASCII character set? 2) C++ supports wchar_t types. But again, this has no defined...
13
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i have come across a situation in my project where i read a text file with some characters greater than hex 0x7f. i need to write character (0xE0) to a new file as an exception. however when i...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.