函数 "fgetc ()" 暂时无法正常工作
the function "fgetc ()" does not work properly in a While
我写了一个小程序,它应该逐个字符地读取文件的内容,但是代码的作用是每次一个字符它都会跳转,就好像它每次都转义一个字符一样,我不明白为什么
我不知道该怎么办
#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;
}
该文件存在并包含 "Hello World !!Your name is Himou and your age is 18",这就是我的预期,但实际结果 "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
是的,这是真的,因为你要求这样做,请参阅我在上面的代码中添加的评论
i dont know what to do
可能您想要类似的东西来写入所有读取的字符:
int c;
while ((c = fgetc(file)) != EOF)
putchar(c);
我写了一个小程序,它应该逐个字符地读取文件的内容,但是代码的作用是每次一个字符它都会跳转,就好像它每次都转义一个字符一样,我不明白为什么
我不知道该怎么办
#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;
}
该文件存在并包含 "Hello World !!Your name is Himou and your age is 18",这就是我的预期,但实际结果 "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
是的,这是真的,因为你要求这样做,请参阅我在上面的代码中添加的评论
i dont know what to do
可能您想要类似的东西来写入所有读取的字符:
int c;
while ((c = fgetc(file)) != EOF)
putchar(c);