请给我一个用C语言编写的完整程序,要求: 1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:序号、姓名、地址、学号,每排每列对齐,没有对齐的地方用空格补齐。 2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑, 3.这个文件要有纵横结构,每横表示一个记录 4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改, 其中学生信息存储在students.dat文件中 能在文件中第一行显示姓名、地址、学号,并且修改后能够自动保存。printf打印的部分要用中文
时间: 2024-03-11 07:43:47 浏览: 26
好的,以下是一个这样的程序示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最大学生数
// 学生信息结构体
typedef struct {
int id; // 序号
char name[20]; // 姓名
char address[50]; // 地址
char number[20]; // 学号
} Student;
// 学生信息数组
Student students[MAX_STUDENTS];
// 学生信息文件名
const char* FILENAME = "students.dat";
// 读取学生信息文件
void read_students() {
FILE* file = fopen(FILENAME, "rb");
if (file == NULL) {
printf("读取文件失败\n");
return;
}
int i = 0;
// 读取第一行,即表头
fscanf(file, "%d %s %s %s\n", &students[i].id, students[i].name, students[i].address, students[i].number);
while (fscanf(file, "%d %s %s %s\n", &students[++i].id, students[i].name, students[i].address, students[i].number) != EOF) {}
fclose(file);
}
// 写入学生信息文件
void write_students() {
FILE* file = fopen(FILENAME, "wb");
if (file == NULL) {
printf("写入文件失败\n");
return;
}
// 写入第一行,即表头
fprintf(file, "%-4s%-12s%-32s%-16s\n", "序号", "姓名", "地址", "学号");
for (int i = 0; i < MAX_STUDENTS && students[i].id != 0; i++) {
// 写入学生信息
fprintf(file, "%-4d%-12s%-32s%-16s\n", students[i].id, students[i].name, students[i].address, students[i].number);
}
fclose(file);
}
// 查找学生信息
int find_student(int id) {
for (int i = 0; i < MAX_STUDENTS && students[i].id != 0; i++) {
if (students[i].id == id) {
return i;
}
}
return -1;
}
// 插入学生信息
void insert_student() {
for (int i = 0; i < MAX_STUDENTS; i++) {
if (students[i].id == 0) {
printf("请输入学生信息(id name address number):");
scanf("%d %s %s %s", &students[i].id, students[i].name, students[i].address, students[i].number);
write_students();
printf("学生信息已插入\n");
return;
}
}
printf("学生信息已满,无法插入\n");
}
// 删除学生信息
void delete_student() {
int id;
printf("请输入要删除的学生id:");
scanf("%d", &id);
int index = find_student(id);
if (index >= 0) {
// 将待删除元素之后的所有元素向前移动一位
for (int i = index; i < MAX_STUDENTS - 1 && students[i + 1].id != 0; i++) {
students[i] = students[i + 1];
}
students[MAX_STUDENTS - 1].id = 0;
write_students();
printf("学生信息已删除\n");
} else {
printf("找不到该学生信息\n");
}
}
// 修改学生信息
void update_student() {
int id;
printf("请输入要修改的学生id:");
scanf("%d", &id);
int index = find_student(id);
if (index >= 0) {
printf("请输入新的学生信息(name address number):");
scanf("%s %s %s", students[index].name, students[index].address, students[index].number);
write_students();
printf("学生信息已修改\n");
} else {
printf("找不到该学生信息\n");
}
}
int main() {
read_students();
printf("学生信息如下:\n");
printf("%-4s%-12s%-32s%-16s\n", "序号", "姓名", "地址", "学号");
for (int i = 0; i < MAX_STUDENTS && students[i].id != 0; i++) {
printf("%-4d%-12s%-32s%-16s\n", students[i].id, students[i].name, students[i].address, students[i].number);
}
while (1) {
printf("请输入操作(1 插入,2 删除,3 修改,0 退出):");
int op;
scanf("%d", &op);
switch (op) {
case 1:
insert_student();
break;
case 2:
delete_student();
break;
case 3:
update_student();
break;
case 0:
return 0;
default:
printf("无效的操作\n");
break;
}
}
}
```
注意,该程序中使用了格式化输出函数printf来保证文件的纵横结构,并且在读写文件时使用了二进制模式。同时,该程序使用了一个学生信息结构体和一个学生信息数组来保存学生信息,使用了一个find_student函数来查找学生信息。为了保证修改后的学生信息能够自动保存,该程序在插入、删除和修改学生信息时都会立即将修改后的学生信息写入文件中。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)
![bat](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)