来源: c primer plus 书中的13.4.1 文件的输入和输出
环境:Windows vs2022
问题:
//addword.c----添加字符到文件的结尾处,使用fscanf 函数
#include
#include
#define MAX 41
int main(void) {
FILE* fp;
char addition[MAX];
if (fp = fopen("D:\\Document\\C_code\\VS2022\\addword\\add.txt", "a+") == NULL) {
fprintf(stderr, "Open the file error.\n");
exit(EXIT_FAILURE);
}
puts("Puts the words you want to add.");
puts("# to quit.");
while ((fscanf(stdin, "%40s", addition) == 1) && (addition[0] != '#')) {
fprintf(fp, "%s\n", addition);
}
puts("Done!");
if (fclose(fp) != 0) {
fprintf(stderr, "Close the file error.\n");
exit(EXIT_FAILURE);
}
return 0;
}
问题解决了,第9行==的运算等级比=高,所以要在 fp=open套上括号