I'm trying to write some characters in this "arq.txt" file and it works well at the beginning, but when I type zero to restart the program, the Do / While loop no longer recognizes zero I can't get out of it anymore. Please help me! Here is my code:
#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);
reads left-over newline from scanf("%d", &c);
.
Try scanf("%c", &ch); getchar();
--> scanf(" %c", &ch);
. Note space