#include <stdio.h> //让用户输入学员的姓名、年龄,并把这些信息按照如下格式
//保存到文件中。 姓名:xxx 年龄:xxx
int main(void){
FILE * file;
char name[32];
char age[32];
int d;
file = fopen("students_info.txt", "a");
if(!file){
printf("文件打开失败,即将退出程序");
system("pause");
return 1;
}
while(1){
printf("请输入姓名:");
scanf("%s", name);
printf("请输入年龄:");
scanf("%s", age);
fprintf(file, "姓名:%s\t年龄:%s\n", name, age);
fclose(file);
printf("1.继续输入\n"
"2.退出\n");
fflush(stdin);
scanf("%d", &d);
if(d == 1){
continue;
}else if(d == 2){
return 0;
}else{
printf("您的输入有误,即将退出程序。");
system("pause");
return 2;
}
}
fclose(file);
return 0;
}
fprintf用法:int fprintf(FILE * stream, const char * format, ...);
最新推荐文章于 2023-03-05 23:11:44 发布