执行/同时不识别字符以结束 C 中的循环



我正在尝试在这个"arq.txt"文件中写入一些字符,它在开始时运行良好,但是当我键入零重新启动程序时,Do/While 循环不再识别零我无法再摆脱它了。请帮帮我!这是我的代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *p;
char ch;
int c = 1, i, cont = 0;
while(c) {
printf("n-------- DIGITANDO VALORES --------n");
p = fopen("arq.txt", "w");
do {
printf("Type: ");
scanf("%c", &ch);
getchar();
putc(ch, p);
cont++;
} while(ch != '0');
fclose(p);
p = fopen("arq.txt", "r");
for(i = 0; i <= cont; i++) {
ch = fgetc(p);
printf("%cn", ch);
}
fclose(p);
printf("If you wish to finish program, please type zero: ");
scanf("%d", &c);
}
}

scanf("%c", &ch);读取scanf("%d", &c);中剩余的换行符。

尝试scanf("%c", &ch); getchar();-->scanf(" %c", &ch);。 笔记空间

最新更新