473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3637
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.