将上面的代码中的fwrite改为用fprintf进行输入信息
可以将fwrite改为fprintf来输入信息,代码如下:
#include <stdio.h>
int main() {
FILE *file;
file = fopen("output.txt", "w");
if (file == NULL) {
printf("无法打开文件\n");
return 1;
}
char name[20];
int age;
printf("请输入您的姓名:");
scanf("%s", name);
fprintf(file, "姓名:%s\n", name);
printf("请输入您的年龄:");
scanf("%d", &age);
fprintf(file, "年龄:%d\n", age);
fclose(file);
return 0;
}
这样修改后,用户输入的姓名和年龄会被写入到output.txt文件中
原文地址: https://www.cveoy.top/t/topic/hL7n 著作权归作者所有。请勿转载和采集!