Do/While 无法识别 C 中结束循环的字符
Do/While not recognizing the character to end loop in 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("%c\n", 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);
。注意 space
我试图在这个“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("%c\n", 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);
。注意 space