来源: c primer plus 书中的13.4.1 文件的输入和输出
环境:Windows vs2022
问题:
能创建文件但是不能在文件中写入,程序运行异常中断
//addword.c----添加字符到文件的结尾处,使用fscanf 函数
#include <stdio.h>
#include <stdlib.h>
#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;
}