I wrote a little program that is supposed to read the contents of a file character by character but what the code does is that it jumps each time a character as if it escapes a caracters each time and I don't understand why
and i dont know what to do
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, age = 18;
char strind[] = "Himou";
FILE *file = NULL;
file = fopen("test.txt", "r+");
if(file != NULL)
{
do
{
printf("%c", fgetc(file));
}while(fgetc(file) != EOF);
fclose(file);
}
else
{
printf("the file couldn't be open");
}
return 0;
}
the file exists and contains "Hello World !!Your name is Himou and your age is 18", so thats what i expected but the actual result "HloWrd!Yu aei io n oraei 8"
it jumps each time a character as if it escapes a caracters each time
do { printf("%c", fgetc(file)); <<< here you read and print a character }while(fgetc(file) != EOF); << here you read again a character and lost it
yes this is true, because you ask for that, see comments I added in the code above
i dont know what to do
probably you want something like that to write all the read characters :
int c;
while ((c = fgetc(file)) != EOF)
putchar(c);
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.