写了一个系统,文件读写出了问题,求帮助
我写了两个cpp 一个cpp是登陆的 一个是系统主程序的然后写着写着,发现文件读写不能用,而且写出来的文件还是乱码
最后实在搞不懂了,求帮助
谢谢!
程序代码:
#include"stdio.h" #include"string.h" #include"stdlib.h" void xxx(); char yonghuming[50]="admin"; //用户名密码为admin char mima[50]="admin"; void login() { char a[50]; char b[50]; while(1) { printf("默认用户名密码均为admin\n"); printf("用户名:"); scanf("%s",&a); getchar(); printf("密码:"); scanf("%s",&b); getchar(); if(!strcmp(yonghuming,a)&&!strcmp(mima,b)) xxx(); else printf("error!\n"); } } void xiugai() { printf("请输入新的用户名:"); scanf("%s",&yonghuming); printf("请输入新的密码:"); scanf("%s",&mima); login(); } void main() { system("color 0A"); login(); }
程序代码:
#include"stdio.h" #include"string.h" #include"stdlib.h" #define N 1000 #define M 5 void xiugai(); /*定义学生结构体*/ struct stu { int num; //学号 char name[20]; //姓名 char sex[20]; //性别 float mark[M]; //分数 } ; struct stu students[N]; int i=0; /*添加学生信息*/ void students_message() { char pan; int j; while(1) { printf("请输入学号:"); scanf("%d",&students[i].num); printf("请输入姓名:"); scanf("%s",&students[i].name); printf("请输入性别:"); scanf("%s",&students[i].sex); for(j=0;j<M;j++) { printf("请输入分数%d:",j+1); scanf("%f",&(students[i].mark[j])); } i++; while(1) { printf("是否继续输入?(Y/N)"); scanf("%s",&pan); getchar(); switch(pan) { case 'N': case 'n': return; case 'Y': case 'y': break; default: printf("错误,请重新输入!\n"); continue; } break; } } } /*修改学生信息*/ void students_recover() { int j,k,t; char xiu[50]; int xiu1; char choise; printf("请选择修改类型:姓名查询(a)/学号查询(b)"); scanf("%s",&choise); switch(choise) { case 'a': printf("请输入姓名:"); scanf("%s",&xiu); for(j=0,k=0;j<N;j++) if(strcmp((const char *)xiu,students[j].name)==0) //查询是否存在该姓名 { printf("请输入新的学号:"); scanf("%d",&students[j].num); printf("请输入新的姓名:"); scanf("%s",&students[j].name); printf("请输入新的性别:"); scanf("%s",&students[j].sex); for(t=0;t<M;t++) { printf("请输入分数%d:",t+1); scanf("%f",&(students[j].mark[t])); } k=1; } if(k) break; else printf("该学生不存在!\n"); system("pause"); break; case 'b': printf("请输入学号:"); scanf("%d",&xiu1); for(j=0,k=0;j<N;j++) if(xiu1==students[j].num) //查询是否存在该学号 { printf("请输入新的学号:"); scanf("%d",&students[j].num); printf("请输入新的姓名:"); scanf("%s",&students[j].name); printf("请输入新的性别:"); scanf("%s",&students[j].sex); for(t=0;t<M;t++) { printf("请输入分数%d:",t+1); scanf("%f",&(students[j].mark[t])); } k=1; } if(k) break; else printf("该学生不存在!\n"); system("pause"); break; default: printf("输入错误!\n"); break; } } /*查询学生信息*/ void students_chaxun1() { char choise; int j,k,t; int cha1; char cha[50]; printf("请选择查询类型:姓名查询(a)/学号查询(b)"); scanf("%s",&choise); switch(choise) { case 'a': printf("请输入姓名:"); //输入姓名查询 scanf("%s",&cha); for(j=0,k=0;j<N;j++) if(strcmp((const char *)cha,students[j].name)==0) { printf("\n\n"); printf("--------------------------------------------------------------------\n"); printf("%10s%10s%8s%8s%8s%8s%8s%8s\n","学号","姓名","性别","成绩1","成绩2","成绩3","成绩4","成绩5"); printf("--------------------------------------------------------------------\n"); printf("%10d%10s%8s",students[j].num,students[j].name,students[j].sex); for(t=0;t<M;t++) printf("%8.2f",students[j].mark[t]); printf("\n"); printf("--------------------------------------------------------------------\n"); printf("\n\n"); system("pause"); k=1; } if(k) break; else printf("不存在\n"); system("pause"); break; case 'b': printf("请输入学号:"); //输入学号查询 scanf("%d",&cha1); for(j=0,k=0;j<N;j++) if(cha1==students[j].num) { printf("\n\n"); printf("--------------------------------------------------------------------\n"); printf("%10s%10s%8s%8s%8s%8s%8s\n","学号","姓名","性别","成绩1","成绩2","成绩3","成绩4","成绩5"); printf("--------------------------------------------------------------------\n"); printf("%10d%10s%8s",students[j].num,students[j].name,students[j].sex); for(t=0;t<M;t++) printf("%8.2f",students[j].mark[t]); printf("\n"); printf("--------------------------------------------------------------------\n"); printf("\n\n"); system("pause"); k=1; } if(k) break; else printf("不存在\n"); system("pause"); default: printf("输入错误!\n"); break; } } /*输出学生信息*/ void students_shuchu() { int j,k; printf("\n\n"); printf("--------------------------------------------------------------------\n"); printf("%10s%10s%8s%8s%8s%8s%8s%8s\n","学号","姓名","性别","成绩1","成绩2","成绩3","成绩4","成绩5"); printf("--------------------------------------------------------------------\n"); for(j=0;j<i;j++) { printf("%10d%10s%8s",students[j].num,students[j].name,students[j].sex); for(k=0;k<M;k++) printf("%8.2f",students[j].mark[k]); printf("\n"); } printf("--------------------------------------------------------------------\n"); printf("\n\n"); system("pause"); } /*删除学生信息*/ void students_delete() { int j,k,l; int del; char del1[50]; printf("请输入学号:"); scanf("%d",&del); printf("请输入姓名:"); scanf("%s",&del1); for(j=0;j<N;j++) if(del==students[j].num) { if(strcmp((const char *)del1,students[j].name)==0) //验证学号以及姓名 { printf("学生信息核对成功!\n"); for(k=j;k<N;k++) { strcpy(students[k].name,students[k+1].name); strcpy(students[k].sex,students[k+1].sex); students[k].num=students[k+1].num; for(l=0;l<M;l++) students[k].mark[l]=students[k+1].mark[l]; } printf("该学生已删除!\n"); i--; } } else { printf("学生信息核对出错或不存在!\n"); break; } system("pause"); } /*读取文件*/ void read_txt() { FILE *fp; int t; if((fp=fopen("message.txt","a+"))==NULL) { printf("不能打开文件!\n"); return; } if(fwrite(&i,sizeof(int),1,fp)!=1) printf("写入文件错误!\n"); for(t=0;t<i;t++) { if (fwrite(&students[t],sizeof(struct stu),1,fp)!=1) printf("写入文件错误!\n"); } fclose(fp); } /*写入文件*/ void write_txt() { FILE *fp; int t; if((fp=fopen("message.txt","w+"))==NULL) { printf("不能打开文件!\n"); return; } if(fwrite(&i,sizeof(int),1,fp)!=1) printf("写入文件错误!\n"); for(t=0;t<i;t++) { if(fwrite(&students[t],sizeof(struct stu),1,fp)!=1) printf("写入文件错误!\n"); } fclose(fp); } /*主函数*/ void xxx() { int choice; read_txt(); while(1) { system("cls"); printf("欢迎进入学生管理系统!\n"); printf("---------------------\n"); printf(" 1.添加学生信息\n"); printf(" 2.修改学生信息\n"); printf(" 3.查询学生信息\n"); printf(" 4.输出学生信息\n"); printf(" 5.删除学生信息\n"); printf(" 6.修改系统密码\n"); printf(" 7.退出学生系统\n"); printf("---------------------\n"); printf("请选择1-7:"); scanf("%d",&choice); switch(choice) { case 1: students_message(); break; case 2: students_recover(); break; case 3: students_chaxun1(); break; case 4: students_shuchu(); break; case 5: students_delete(); break; case 6: xiugai(); break; case 7: printf("已成功退出学生管理系统\n"); exit(0); break; default: printf("错误\n"); getchar(); system("pause"); } write_txt(); } }