主题:求助,作业无法攻克,在文件读写上的问题
题目:学生档案管理程序
要点:用文本命令行界面实现;用文件保存各种档案信息;能实现档案信息的增、删、改、查等功能。
要求:建立文件操作类、界面显示类及相关接口类,用文件读写实现。学生信息用文件保存,然后可以从文件中读取出某学生信息并显示,可以删除某学生信息。
个人在文件读写方面太差,每次保存信息时就出现错误,求指教,或有相似的程序源码参考也可以。
过几天就要交课程设计了,求高手指导!!!
个人初编的代码:
[code=c]
#include <string>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <fstream>
using namespace std;
typedef struct stu
{
char name[30];
char ID[30];
char sex[30];
char year[30];
char col[30];
char maj[30];
char birth[30];
char tel[30];
char add[30];
struct stu *next;
}STU;
class student
{
private:
STU *p;
STU *q;
STU *t;
public:
student(STU *m); //初始化
void xinzeng(); //增加成员
void chazhao(); //查找
void show(); //显示所有
void xiugai(); //修改
void empty(); //清空所有
void del(); //删除
void menu(); //菜单界面
void infile(char *s);
void outfile(char s[30],STU *l);
};
student::student(STU *m) //初始化
{
int i;
t=p=m;
for(i=0;i<30;i++)
{
p->name[i]=' ';
p->ID[i]=' ';
p->birth[i]=' ';
p->tel[i]=' ';
p->add[i]=' ';
p->col[i]=' ';
p->maj[i]=' ';
p->sex[i]=' ';
p->year[i]=' ';
}
}
void student::infile(char *s) //读文件内容
{
ifstream inf;
STU l;
char *d=".txt";
char file[30];
file[30]=*s;
strcat(file,d);
inf.open(file,ios::in|ios::binary);
if(!inf)
{cout<<"失败!!"<<endl;
}
else
{
inf.read((char*)&l,sizeof(l));
cout<<"姓名:"<<l.name<<endl;
cout<<"学号:"<<l.ID<<endl;
cout<<"性别:"<<l.sex<<endl;
cout<<"入学年份:"<<l.year<<endl;
cout<<"学院:"<<l.col<<endl;
cout<<"专业班级:"<<l.maj<<endl;
cout<<"出生年月:"<<l.birth<<endl;
cout<<"联系电话:"<<l.tel<<endl;
cout<<"家庭住址:"<<l.add<<endl;
}
inf.close();
}
void student::outfile(char s[30],STU *l) //写文件内容
{
ofstream outf;
/* char *d=".txt";
char file[30];
file[30]=s[30];
strcat(file,d);*/
outf.open("123.txt",ios::out|ios::binary);
if(!outf)
{cout<<"失败!!"<<endl;
}
else
{
outf.write((char*)l,sizeof(l));
}
outf.close();
}
void student::del() //删除
{ STU *k;
k=new STU;
k=p=q;
char n[20];
cout<<"请输入学生学号号:"<<endl;
cin>>n;
while (p!=t&&(strcmp(p->ID,n)!=0)!=0)
{k=p;
p=p->next;
}
if (p==t)
{cout<<"查无此人!!"<<endl;
}
else
{if (p==q)
q=q->next;
else
k->next=p->next;
free(p);
}
}
void student::chazhao() //查找
{ p=q;
char n[20];
cout<<"请输入学生学号:"<<endl;
cin>>n;
while (p!=t&&(strcmp(p->ID,n)!=0)!=0)
{p=p->next;
}
if (p==t)
{cout<<"查无此人!!"<<endl;
}
else
{ cout<<"姓名:"<<p->name<<endl;
cout<<"学号:"<<p->ID<<endl;
cout<<"性别:"<<p->sex<<endl;
cout<<"入学年份:"<<p->year<<endl;
cout<<"学院:"<<p->col<<endl;
cout<<"专业班级:"<<p->maj<<endl;
cout<<"出生年月:"<<p->birth<<endl;
cout<<"联系电话:"<<p->tel<<endl;
cout<<"家庭住址:"<<p->add<<endl;
}
cout<<"按任意键返回!!"<<endl;
}
void student::xiugai() //修改
{
p=q;
char n[20];
cout<<"请输入学生学号:"<<endl;
cin>>n;
while (p!=t&&(strcmp(p->ID,n)!=0)!=0)
{p=p->next;
}
if (p==t)
{cout<<"查无此人!!"<<endl;
}
else
{cout<<"姓名";
cin>>p->name;
cout<<"学号:";
cin>>p->ID;
cout<<"性别:";
cin>>p->sex;
cout<<"入学年份:";
cin>>p->year;
cout<<"学院:";
cin>>p->col;
cout<<"专业班级:";
cin>>p->maj;
cout<<"出生年月:";
cin>>p->birth;
cout<<"联系电话:";
cin>>p->tel;
cout<<"家庭住址";
cin>>p->add;
}
cout<<"按任意键返回!!"<<endl;
}
void student::xinzeng() //增加成员
{
q= new STU;
q->next=p;
p=q;
cout<<"请输入新学生信息:"<<endl;
cout<<"姓名";
cin>>p->name;
cout<<"学号:";
cin>>p->ID;
cout<<"性别:";
cin>>p->sex;
cout<<"入学年份:";
cin>>p->year;
cout<<"学院:";
cin>>p->col;
cout<<"专业班级:";
cin>>p->maj;
cout<<"出生年月:";
cin>>p->birth;
cout<<"联系电话:";
cin>>p->tel;
cout<<"家庭住址:";
cin>>p->add;
cout<<"按任意键返回!!"<<endl;
outfile(p->ID,p);
}
void student::show() //显示所有
{
p=q;
cout<<"所有学生信息如下:"<<endl;
while (q!=t)
{
cout<<"姓名:"<<q->name<<endl;
cout<<"学号:"<<q->ID<<endl;
cout<<"性别:"<<q->sex<<endl;
cout<<"入学年份:"<<q->year<<endl;
cout<<"学院:"<<q->col<<endl;
cout<<"专业班级:"<<q->maj<<endl;
cout<<"出生年月:"<<q->birth<<endl;
cout<<"联系电话:"<<q->tel<<endl;
cout<<"家庭住址:"<<q->add<<endl;
q=q->next;
// getch();
}
q=p;
}
void student::empty() //清空所有
{
while (q!=t)
{p=q;
q=q->next;
free(p);
}
cout<<"所有信息已删除!" ;
}
void menu() //菜单界面
{
cout<<"\n ******1:加入新学生信息! 2:修改学生信息!******"<<endl;
cout<<"\n ******3:查找学生信息! 4:显示所有学生信息!**"<<endl;
cout<<"\n ******5:删除学生信息! 6:删除所有学生信息!**"<<endl;
cout<<"\n ****** 7:退出! ******"<<endl;
}
void main()
{
STU *pa;
char i;
pa=new STU;
menu();
student a(pa);
while(i=getch())
{
switch(i)
{
case'1':system("cls");a.xinzeng();getch();system("cls");menu();break;
case'2':system("cls");a.xiugai();getch();system("cls");menu();break;
case'3':system("cls");a.chazhao();getch();system("cls");menu();break;
case'4':system("cls");a.show();getch();system("cls");menu();break;
case'5':system("cls");a.del();getch();system("cls");menu();break;
case'6':system("cls");a.empty();getch();system("cls");menu();break;
case'7':break;
}
if(i=='7') break;
}
}
[/code]
要点:用文本命令行界面实现;用文件保存各种档案信息;能实现档案信息的增、删、改、查等功能。
要求:建立文件操作类、界面显示类及相关接口类,用文件读写实现。学生信息用文件保存,然后可以从文件中读取出某学生信息并显示,可以删除某学生信息。
个人在文件读写方面太差,每次保存信息时就出现错误,求指教,或有相似的程序源码参考也可以。
过几天就要交课程设计了,求高手指导!!!
个人初编的代码:
[code=c]
#include <string>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <fstream>
using namespace std;
typedef struct stu
{
char name[30];
char ID[30];
char sex[30];
char year[30];
char col[30];
char maj[30];
char birth[30];
char tel[30];
char add[30];
struct stu *next;
}STU;
class student
{
private:
STU *p;
STU *q;
STU *t;
public:
student(STU *m); //初始化
void xinzeng(); //增加成员
void chazhao(); //查找
void show(); //显示所有
void xiugai(); //修改
void empty(); //清空所有
void del(); //删除
void menu(); //菜单界面
void infile(char *s);
void outfile(char s[30],STU *l);
};
student::student(STU *m) //初始化
{
int i;
t=p=m;
for(i=0;i<30;i++)
{
p->name[i]=' ';
p->ID[i]=' ';
p->birth[i]=' ';
p->tel[i]=' ';
p->add[i]=' ';
p->col[i]=' ';
p->maj[i]=' ';
p->sex[i]=' ';
p->year[i]=' ';
}
}
void student::infile(char *s) //读文件内容
{
ifstream inf;
STU l;
char *d=".txt";
char file[30];
file[30]=*s;
strcat(file,d);
inf.open(file,ios::in|ios::binary);
if(!inf)
{cout<<"失败!!"<<endl;
}
else
{
inf.read((char*)&l,sizeof(l));
cout<<"姓名:"<<l.name<<endl;
cout<<"学号:"<<l.ID<<endl;
cout<<"性别:"<<l.sex<<endl;
cout<<"入学年份:"<<l.year<<endl;
cout<<"学院:"<<l.col<<endl;
cout<<"专业班级:"<<l.maj<<endl;
cout<<"出生年月:"<<l.birth<<endl;
cout<<"联系电话:"<<l.tel<<endl;
cout<<"家庭住址:"<<l.add<<endl;
}
inf.close();
}
void student::outfile(char s[30],STU *l) //写文件内容
{
ofstream outf;
/* char *d=".txt";
char file[30];
file[30]=s[30];
strcat(file,d);*/
outf.open("123.txt",ios::out|ios::binary);
if(!outf)
{cout<<"失败!!"<<endl;
}
else
{
outf.write((char*)l,sizeof(l));
}
outf.close();
}
void student::del() //删除
{ STU *k;
k=new STU;
k=p=q;
char n[20];
cout<<"请输入学生学号号:"<<endl;
cin>>n;
while (p!=t&&(strcmp(p->ID,n)!=0)!=0)
{k=p;
p=p->next;
}
if (p==t)
{cout<<"查无此人!!"<<endl;
}
else
{if (p==q)
q=q->next;
else
k->next=p->next;
free(p);
}
}
void student::chazhao() //查找
{ p=q;
char n[20];
cout<<"请输入学生学号:"<<endl;
cin>>n;
while (p!=t&&(strcmp(p->ID,n)!=0)!=0)
{p=p->next;
}
if (p==t)
{cout<<"查无此人!!"<<endl;
}
else
{ cout<<"姓名:"<<p->name<<endl;
cout<<"学号:"<<p->ID<<endl;
cout<<"性别:"<<p->sex<<endl;
cout<<"入学年份:"<<p->year<<endl;
cout<<"学院:"<<p->col<<endl;
cout<<"专业班级:"<<p->maj<<endl;
cout<<"出生年月:"<<p->birth<<endl;
cout<<"联系电话:"<<p->tel<<endl;
cout<<"家庭住址:"<<p->add<<endl;
}
cout<<"按任意键返回!!"<<endl;
}
void student::xiugai() //修改
{
p=q;
char n[20];
cout<<"请输入学生学号:"<<endl;
cin>>n;
while (p!=t&&(strcmp(p->ID,n)!=0)!=0)
{p=p->next;
}
if (p==t)
{cout<<"查无此人!!"<<endl;
}
else
{cout<<"姓名";
cin>>p->name;
cout<<"学号:";
cin>>p->ID;
cout<<"性别:";
cin>>p->sex;
cout<<"入学年份:";
cin>>p->year;
cout<<"学院:";
cin>>p->col;
cout<<"专业班级:";
cin>>p->maj;
cout<<"出生年月:";
cin>>p->birth;
cout<<"联系电话:";
cin>>p->tel;
cout<<"家庭住址";
cin>>p->add;
}
cout<<"按任意键返回!!"<<endl;
}
void student::xinzeng() //增加成员
{
q= new STU;
q->next=p;
p=q;
cout<<"请输入新学生信息:"<<endl;
cout<<"姓名";
cin>>p->name;
cout<<"学号:";
cin>>p->ID;
cout<<"性别:";
cin>>p->sex;
cout<<"入学年份:";
cin>>p->year;
cout<<"学院:";
cin>>p->col;
cout<<"专业班级:";
cin>>p->maj;
cout<<"出生年月:";
cin>>p->birth;
cout<<"联系电话:";
cin>>p->tel;
cout<<"家庭住址:";
cin>>p->add;
cout<<"按任意键返回!!"<<endl;
outfile(p->ID,p);
}
void student::show() //显示所有
{
p=q;
cout<<"所有学生信息如下:"<<endl;
while (q!=t)
{
cout<<"姓名:"<<q->name<<endl;
cout<<"学号:"<<q->ID<<endl;
cout<<"性别:"<<q->sex<<endl;
cout<<"入学年份:"<<q->year<<endl;
cout<<"学院:"<<q->col<<endl;
cout<<"专业班级:"<<q->maj<<endl;
cout<<"出生年月:"<<q->birth<<endl;
cout<<"联系电话:"<<q->tel<<endl;
cout<<"家庭住址:"<<q->add<<endl;
q=q->next;
// getch();
}
q=p;
}
void student::empty() //清空所有
{
while (q!=t)
{p=q;
q=q->next;
free(p);
}
cout<<"所有信息已删除!" ;
}
void menu() //菜单界面
{
cout<<"\n ******1:加入新学生信息! 2:修改学生信息!******"<<endl;
cout<<"\n ******3:查找学生信息! 4:显示所有学生信息!**"<<endl;
cout<<"\n ******5:删除学生信息! 6:删除所有学生信息!**"<<endl;
cout<<"\n ****** 7:退出! ******"<<endl;
}
void main()
{
STU *pa;
char i;
pa=new STU;
menu();
student a(pa);
while(i=getch())
{
switch(i)
{
case'1':system("cls");a.xinzeng();getch();system("cls");menu();break;
case'2':system("cls");a.xiugai();getch();system("cls");menu();break;
case'3':system("cls");a.chazhao();getch();system("cls");menu();break;
case'4':system("cls");a.show();getch();system("cls");menu();break;
case'5':system("cls");a.del();getch();system("cls");menu();break;
case'6':system("cls");a.empty();getch();system("cls");menu();break;
case'7':break;
}
if(i=='7') break;
}
}
[/code]