Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 20th, 2005, 5:13 AM   #1
anant_tickoo
Programmer
 
Join Date: Sep 2005
Posts: 50
Rep Power: 0 anant_tickoo is an unknown quantity at this point
bug that i can't find

this works fine but it has a problem that it writes an extra char "ÿ" in end of the copied file then exits.






#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int main()
{
int a,b;
char ch;
FILE *fp,*cfp;

//cheak file exists
if((fp=(fopen("sunny.txt","r")))==NULL)
{
printf("Were the fuck is file");
}

//cheak that file has created
if((cfp=fopen("sunny.out","w"))==NULL)
{
printf("window sucks");
}

while(feof(fp)==0)
{
ch=fgetc(fp);
fputc(ch,cfp);
printf("%c",ch);

}
fclose(fp);
fclose(cfp);
}
anant_tickoo is offline   Reply With Quote
Old Sep 20th, 2005, 5:47 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
Please read the posts on how to post questions. In particular, learn how to use code tags.

It's not usually a good idea to have a loop of the form
while (feof(fp) == 0)
{
    ch = fgetc(fp);
    fputc(ch, cfp);
}
as fgetc() will return EOF if it encounters an end of file, and it should not be output.

You might change your code to this ....
while (feof(fp) == 0)
{
    ch = fgetc(fp);
    if (ch != EOF) fputc(ch, cfp);
}
or, more simply, to this ...
while ((ch = fgetc(fp)) != EOF)
{
    fputc(ch, cfp);
}
grumpy is offline   Reply With Quote
Old Sep 20th, 2005, 8:20 AM   #3
anant_tickoo
Programmer
 
Join Date: Sep 2005
Posts: 50
Rep Power: 0 anant_tickoo is an unknown quantity at this point
thanks grumpy


i understand the bug and solution
anant_tickoo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC