المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Write and read the same file in the same C program?



C++ Programming
09-30-2009, 02:00 PM
I tried out this code. The problem is that for example I input "Book" to the file so it displays 4characters of the ASCII '1'.
If I enter a three letter word it displays that character three times?
Here is the code.


#include
#include
#include

char d;
main()
{
{
FILE *ft;
ft=fopen("test.txt","w");
char ch;
while(ch=getche())
{
if (ch=='\r')
break;
putc(ch,ft);}
fclose(ft);
}
system("cls");
printf("Read?");
d=getch();
system("cls");
if(d=='y')
goto read;
read:{
FILE *fp;
fp=fopen("test.txt","r+");
char chr;
while(chr=fgetc(fp)!=EOF)
printf("%d",chr);
fclose(fp);
}

}