473,763 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("encrypte d.txt", "r");
outfile = fopen("plain.tx t", "w");

while(!feof(inf ile)) {
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 3686
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("encrypte d.txt", "r");
outfile = fopen("plain.tx t", "w");
What if either (or both) of these files fails to open?
>
while(!feof(inf ile)) {
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(infil e))
{
fprintf(stderr, "error reading %s\n", argv[1]);
}
if(ferror(outfi le))
{
fprintf(stderr, "error writing %s\n", argv[2]);
}
if(fclose(outfi le) != 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("encrypte d.txt", "r");
outfile = fopen("plain.tx t", "w");

while(!feof(inf ile)) {
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("encrypte d.txt", "r");
outfile = fopen("plain.tx t", "w");

while(!feof(inf ile)) {
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("encrypte d.txt", "r");
if (!infile) {
puts("Can't open 'encryped.txt'" );
return 0;
}
outfile = fopen("plain.tx t", "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(C9 9, txt)
<http://www.dinkumware. com/c99.aspx (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:com p.lang.c:Introd uction>

** 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
14429
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 that ..dat file and do something based on it's contents at their end. My application will run on a windows xp machine but client should be able to read this dat file in unix box. So, they want to know what will be the end of line character...
18
4631
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 has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my code so far to remove the small y: Private Sub Button2_Click(ByVal sender As System.Object, ByVal...
11
3669
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 EOF/error), returns n-1 characters plus a appended null character.
2
2454
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 it to the ouput file but if not found write all the line to the output file so i read line by line cin.get(input,line) if(line != s) output<<line
1
1999
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; hFile =CreateFile"D:\\test.text",GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
2
2945
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", to write, lets say, 1000 bytes in binary. The output contained 1022 bytes, even though fwrite told me (returned) that it only wrote 1000 bytes.
9
2592
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 = null for not validating DTD. Then i get one particular node through getElementBytagname method. After find out the node i have add the attribute fot the node. then i save the xml file in particular path through xmldocumenttype.save() method. After i...
9
2923
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 character set? For instance, it might not be a unicode character set?
13
3901
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 attempt to write this via "Console.Write" or "filestream.Write" it seems the value changes. most of the output file is in text mode. if i view the original file in binary mode i see the character i'm having issue with as "e0 00" but when i...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.