用c++编写一个程序,实现以下功能:输入一系列学生数据,将学生信息写入二进制文件中
时间: 2023-05-27 13:07:58 浏览: 122
C++编程实现学生基本信息管理程序
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[20];
int age;
float score;
} Student;
int main() {
FILE *fp;
Student stu;
int count = 0;
if ((fp = fopen("students.dat", "wb")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
printf("Enter student information (name, age, score):\n");
while (1) {
scanf("%s %d %f", stu.name, &stu.age, &stu.score);
if (stu.score < 0) {
break;
}
fwrite(&stu, sizeof(Student), 1, fp);
count++;
}
fclose(fp);
printf("%d students have been written to file.\n", count);
return 0;
}
```
阅读全文