简体   繁体   中英

Weird getchar() output on file C program

So, I was tyring to write this code where an output txt file would copy everything the user typed, here is the code:

int main(){
FILE *txt = fopen("Output.txt","a");
char *Pcarac;
char carac;
Pcarac = &carac;

while(carac!='\n'){
    carac = getch();
    fprintf(txt,Pcarac);
}
return 0;
 }

However i always get these weird outputs ᝡ⋿਍7愢7ഢᜊ⋿ᝡ⋿ᝢ⋿ᝣ⋿ᝤ⋿ᜍ⋿ᝡ⋿ᝢ⋿ᝣ⋿ᜍ⋿ᜍ⋿. Why is this happening and how can i solve it? thanks

try, putc(char,FILE*)

int main(){
FILE *txt = fopen("Output.txt","a");
char *Pcarac;
char carac;
Pcarac = &carac;

while(carac!='\n'){
    carac = getch();
    putc(*Pcarac, txt);
}
return 0;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM