This commit is contained in:
hairrrrr
2020-02-12 13:56:49 +08:00
parent 63f913871c
commit cf5994896a
3 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#include<stdio.h>
#include<stdlib.h>
#include "student.h"
void getList(Student* Stu, int num);
int save(Student* Stu, int num);
int main(void) {
int num = 0;
printf("请输入学生数量:\n");
scanf("%d", &num);
Student* Stu = (Student*)malloc(sizeof(Student) * num);
getList(Stu, num);
if (save(Stu, num)) {
printf("保存成功\n");
}
else
printf("保存失败\n");
return 0;
}
void getList(Student* Stu, int num) {
char format[STR_LEN];
sprintf(format, "%%%ds", STR_LEN - 1);
//向 format 中写入 %19s
//%%%ds 前两个%的意义是写入%
//%d 输出后面的参数19
//最终写入的就是 %19s
int i = 0;
for (i = 0; i < num; i++) {
printf("第 %d 个学生 \n", i + 1);
printf("\t姓名:");
scanf(format, Stu[i].name);
printf("\t性别0-男 1-女 2-其他):");
scanf("%d", &Stu[i].gender);
printf("\t年龄:");
scanf("%d", &Stu[i].age);
//记得用scanf时何时用 &
}
}
int save(Student* Stu, int num) {
int ret = -1;
FILE* fp = fopen("student.data", "wx");
if (fp) {
ret = fwrite(Stu, sizeof(Student), num, fp);
//记录 fwrite 的返回值:写入文件的学生个数
fclose(fp);
}
else
printf("文件打开失败!");
return (ret == num);
//如果写入的个数不等于总人数返回0
}

View File

@@ -0,0 +1,4 @@
程序中向文件中写入2进制文本
linux 下可以用 `od` 命令打开文件

View File

@@ -0,0 +1,11 @@
#ifndef _STUDENT_H_
#define _STUDENT_H_
#define STR_LEN 20
typedef struct _student {
char name[STR_LEN];
char gender;
char age;
}Student;
#endif