写一个c文件,从键盘输入3个学生信息,把学生信息写入文件保存
时间: 2023-03-27 11:00:24 浏览: 72
好的,我可以回答这个问题。以下是一个示例 C 代码,可以从键盘输入 3 个学生信息,并将其写入文件保存:
```c
#include <stdio.h>
struct student {
char name[20];
int age;
float score;
};
int main() {
FILE *fp;
struct student s[3];
int i;
// 从键盘输入学生信息
for (i = ; i < 3; i++) {
printf("请输入第 %d 个学生的姓名、年龄和成绩:", i+1);
scanf("%s %d %f", s[i].name, &s[i].age, &s[i].score);
}
// 打开文件并写入学生信息
fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("无法打开文件!\n");
return 1;
}
for (i = ; i < 3; i++) {
fprintf(fp, "%s %d %.2f\n", s[i].name, s[i].age, s[i].score);
}
fclose(fp);
printf("学生信息已保存到文件 students.txt 中。\n");
return ;
}
```
注意,这只是一个示例代码,实际应用中可能需要根据具体需求进行修改。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)