请给我一个用C语言编写的完程序,要求: 1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:序号、姓名、地址、学号,每排每列对齐,没有对齐的地方用空格补齐。 2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑, 3.这个文件要有纵横结构,每横表示一个记录 4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改, 其中学生信息存储在students.dat文件中,每个学生信息占据一行,每个字段用空格分隔,每个字段的最大宽度为MAX_NAME_LEN。 代码中能够实现插入、删除和修改学生信息的功能。 能在文件中第一行显示姓名、地址、学号,并且修改后能够自动保存
时间: 2024-03-11 11:43:52 浏览: 98
很抱歉,我无法为您提供完整的代码,因为这超出了我的能力范围。但我可以提供一些指导,希望对您有所帮助。
首先,您需要定义一个结构体来存储学生信息,包括序号、姓名、地址和学号。您可以使用以下代码完成这一步骤:
```
#define MAX_NAME_LEN 20
struct student {
int id;
char name[MAX_NAME_LEN];
char address[MAX_NAME_LEN];
char number[MAX_NAME_LEN];
};
```
接下来,您需要编写函数来读取和写入数据文件,包括打开、关闭、读取和写入文件。您可以使用以下代码完成这一步骤:
```
void read_students(struct student *students, int len, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file\n");
exit(1);
}
for (int i = 0; i < len; i++) {
fscanf(fp, "%d %s %s %s\n", &students[i].id, students[i].name, students[i].address, students[i].number);
}
fclose(fp);
}
void write_students(struct student *students, int len, char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error opening file\n");
exit(1);
}
for (int i = 0; i < len; i++) {
fprintf(fp, "%d %s %s %s\n", students[i].id, students[i].name, students[i].address, students[i].number);
}
fclose(fp);
}
```
然后,您需要编写函数来插入、删除和修改学生信息。以下是一个简单的示例,您可以根据需要进行修改:
```
void insert_student(struct student *students, int len) {
students[len].id = len + 1;
printf("Enter name: ");
scanf("%s", students[len].name);
printf("Enter address: ");
scanf("%s", students[len].address);
printf("Enter number: ");
scanf("%s", students[len].number);
}
void delete_student(struct student *students, int len, int index) {
for (int i = index; i < len - 1; i++) {
students[i] = students[i + 1];
}
}
void modify_student(struct student *students, int index) {
printf("Enter new name: ");
scanf("%s", students[index].name);
printf("Enter new address: ");
scanf("%s", students[index].address);
printf("Enter new number: ");
scanf("%s", students[index].number);
}
```
最后,您需要编写一个主函数来调用上述函数,并进行交互式操作。以下是一个简单的示例,您可以根据需要进行修改:
```
#define MAX_STUDENTS 100
int main() {
struct student students[MAX_STUDENTS];
int len = 0;
read_students(students, len, "students.dat");
len++;
printf("ID Name Address Number\n");
printf("-------------------------------------------------------\n");
for (int i = 0; i < len; i++) {
printf("%-4d %-20s %-20s %-20s\n", students[i].id, students[i].name, students[i].address, students[i].number);
}
char choice;
int index;
while (1) {
printf("\nEnter choice (I for insert, D for delete, M for modify, Q for quit): ");
scanf(" %c", &choice);
switch (choice) {
case 'I':
insert_student(students, len);
len++;
break;
case 'D':
printf("Enter index to delete: ");
scanf("%d", &index);
delete_student(students, len, index - 1);
len--;
break;
case 'M':
printf("Enter index to modify: ");
scanf("%d", &index);
modify_student(students, index - 1);
break;
case 'Q':
write_students(students, len, "students.dat");
return 0;
default:
printf("Invalid choice\n");
break;
}
printf("\nID Name Address Number\n");
printf("-------------------------------------------------------\n");
for (int i = 0; i < len; i++) {
printf("%-4d %-20s %-20s %-20s\n", students[i].id, students[i].name, students[i].address, students[i].number);
}
}
}
```
希望这些代码片段对您有所帮助,祝您编写成功!
阅读全文