我正在尝试读取名为的文件pp.txt
的内容并在命令行上显示它的内容。我的代码是:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *f;
float x;
f=fopen("pp.txt", "r");
if((f = fopen("pp.txt", "r")) == NULL)
{
fprintf(stderr, "Sorry! Can't read %s\n", "TEST1.txt");
}
else
{
printf("File opened successfully!\n");
}
fscanf(f, " %f", &x);
if (fscanf(f, " %f ", &x) != 1) {
fprintf(stderr, "File read failed\n");
return EXIT_FAILURE;
}
else
{
printf("The contents of file are: %f \n", x);
}
fclose(f);
return 0;
}
编译后我得到File opened successfully!File read failed
. 我的内容pp.txt
是34.5。谁能告诉我我哪里出错了?