#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
typedef struct student{
char name[10];
int num;
int score;
struct student* pnext;
}STU;
void input(STU* phead){
STU* node = NULL;
node = (STU*)malloc(sizeof(STU));
node->pnext = NULL;
printf("请输入该同学的姓名
");
scanf("%s",node->name);
printf("请输入该同学的学号
");
scanf("%d",&node->num);
printf("请输入该同学的成绩
");
scanf("%d",&node->score);
FILE* fp = fopen("e:\Student_info.dat","a");
if(fp == NULL)
printf("文件打开失败
");
if(phead->pnext == NULL){
phead->pnext = node;
fwrite(phead->pnext,sizeof(STU),1,fp);
}
else{
while(phead->pnext != NULL){
phead = phead->pnext;
}
phead->pnext = node;
fwrite(phead->pnext,sizeof(STU),1,fp);
}
fclose(fp);
}
void viewer(STU* phead){
FILE* fp = fopen("e:\Student_info.dat","r");
STU* plist = NULL;
plist = (STU*)malloc(sizeof(STU));
plist->pnext = NULL;
fread(plist,sizeof(STU),1,fp);
if(fp == NULL){
printf("未找到数据文件
");
Sleep(3000);
}
else{
while(feof(fp) == 0){
printf("该同学的姓名为: %s
" , plist->name);
printf("该同学的学号为: %d
" , plist->num);
printf("该同学的成绩为: %d
" , plist->score);
printf("
");
fread(plist,sizeof(STU),1,fp);
}
printf("请按任意键进行下一步操作
");
getch();
}
}
void menu(){
printf("1.录入信息
");
printf("2.查看信息
");
printf("0.退出
");
}
int main(int argc,char** argv){
system("color 1e");
int select;
STU* head = NULL;
head = (STU*)malloc(sizeof(STU));
head->pnext = NULL;
while(1){
system("cls");
menu();
printf("请输入您要选择的功能
");
printf("您选择的功能是:
");
scanf("%d",&select);
switch(select)
{
case 1: input(head);
break;
case 2: viewer(head);
break;
case 0: exit(1);
break;
default : printf("输入无效,请重新输入
");
break;
}
}
}