c++文件输入输出出现错误
#include<fstream>
#include<iostream>
#include<string.h>
#include<string>
#include<iomanip>
using namespace std;
class WG//物业管理
{ int No;//编号
string name; //姓名
string telphone; //电话号码
int number; //楼栋号
int homenum; //房间号
char time[10]; //落户时间
public:
void setNo(int N)
{
No=N;
};
void setname(string na)
{
name=na;
};
void settelphone(string t)
{
telphone=t;
};
void setnumber(int n)
{
number=n;
};
void sethomenum(int num)
{
homenum=num;
};
void settime(char ti[])
{
strcpy(time,ti);
};
int getNo()
{
return No;
};
string getname()
{
return name;
};
string gettelphone() //电话号码
{
return telphone;
};
int getnumber() //楼栋号
{
return number;
};
int gethomenum() //房间号
{
return homenum;
};
char* gettime() //落户时间
{
return time;
};
};
/*操作类*/
int n,x;
class compute
{ int FLAG;
int ns;
WG na[10];
int addns;
public:
void getdata(); //输入数据
void disp(); //显示
void initdisp(); //初始化
int usechoice(); //根据用户选择获得不同的操作
void adddate(); //添加信息
void namefind(string cname ); //按姓名查找
void dispone(int i); //显示一个住户的信息
string inputonename();//输入一个住户的名字
void tongji();//统计
void huifu(string c_name);//恢复
void change();//修改
void save();//保存
void dele(string cname); //删除住户记录
void quit();
void build();
void addn();
void readfile();
void addx();
void savefile();
void deletx();
WG hf[10];
//退出系统
compute()
{
ns=0;
}
int getns()
{
return ns;
}
};
void compute::build () /*该函数用于创造保存数据的文件*/
{
fstream fs("data1.txt", ios_base::in);/*关于是否使用该种形式打开文件待议*/
if (!fs) /*如果不存在该文件则创造文件*/
{
fs.close();
fs.open("data1.txt", ios_base::out); /*创建date9.txt文件*/
fs.close(); /*若不关闭文件可能造成严重的后果*/
ofstream outfile2("count1.txt",ios::out); /*创建date10,并使该文件与outfile2对应*/
int n=0; /*n为计数器,功能不明*/
outfile2<<n;
outfile2.close();
ofstream outfile("count2.txt",ios::out); /*创建date1,并使该文件与outfile对应*/
int x=0;
outfile<<x;
ofstream ofile("data2.txt",ios::binary); /*创建date12,并使该文件与ofile对应*/
outfile.close();
ofile.close();
}
}
void compute::addn() /*增加计数器的数值,该程序展现了如何向系统写入和写出数据*/
{
ifstream infile("count1.txt",ios::in);
infile>>n;
infile.close();
n+=1;
ofstream outfile("count1.txt",ios::out);
outfile<<n;
outfile.close();
} /*计数器的功能是计算系统储存了多少组货物的数据*/
void compute::addx() /*增加计数器2的数值*/
{
ifstream infile("count2.txt",ios::in);
infile>>x;
infile.close();
x+=1;
ofstream outfile("count2.txt",ios::out);
outfile<<x;
outfile.close();
} /*计数器的功能是计算系统删除了多少组货物的数据*/
void compute::deletx() /*增加计数器2的数值*/
{
ifstream infile("count2.txt",ios::in);
infile>>x;
infile.close();
x-=1;
ofstream outfile("count2.txt",ios::out);
outfile<<x;
outfile.close();
} /*计数器的功能是计算系统删除了多少组货物的数据*/
void compute::readfile() /*用于读取文件数据*/
{
ifstream infile1("data1.txt",ios::in|ios::out); /*将infile1与date2结合到一起*/
if(!infile1) /*若程序打开失败*/
{cerr<<"open errorr"<<endl;
cout<<"按任意键加回车继续"<<endl;
system("PAUSE");
}
ifstream infile("count1.txt",ios::in|ios::out); /*读取n的值,计数器的值*/
infile>>n;
infile.close();
for(int i=0;i<n;i++)
infile1>>na[i].getNo>>na[i].getname>>na[i].gettelphone>>na[i].getnumber>>na[i].gethomenum>>na[i].gettime;
infile1.close();
ifstream ifile("count2.txt",ios::in|ios::out); /*读取x的值,另一个计数器的值*/
ifile>>x;
ifile.close();
ifstream iofile("data2.txt",ios::in|ios::out); /*读取已经被删除的数据*/
if(!iofile) /*若程序打开失败*/
{cerr<<"open errorr"<<endl;
cout<<"按任意键加回车继续"<<endl;
system("PAUSE");
menu();
}
for(int j=0;j<x;j++)
iofile<<hf[i].getNo<<hf[i].getname<<hf[i].gettelphone<<hf[i].getnumber<<hf[i].gethomenum<<hf[i].gettime;
iofile.close();
} /*每调用一次该函数,就将文件里的所有数据全部付给Employee数组元素*/
void compute::savefile() /*用于将已向程序输入的数据保存到文件中*/
{
int i; /*改变量用于控制循环*/
ofstream iofile("data1.txt",ios::in|ios::out); /*打开date2,并于iofile相对应*/
for(i=0;i<n;i++)
iofile<<setw(10)<<hf[i].getNo<<hf[i].getname<<hf[i].gettelphone<<hf[i].getnumber<<hf[i].gethomenum<<hf[i].gettime<<endl; //书写二进制文件
iofile.close();
ofstream outfile("count1.txt",ios::out|ios::in); /*更新计数器数值*/
outfile<<n;
outfile.close();
int j;
ofstream ifile("data2.txt",ios::in|ios::out); /*将删除后的数据储存到date9中*/
for(j=0;j<x;j++)
ifile<<setw(10)<<hf[j].getNo<<setw(8)<<hf[j].getname<<setw(8)<<hf[j].gettelphone<<setw(8)<<hf[j].number<<setw(8)<<hf[j].gethomenum<<setw(8)<<hf[j].gettime<<endl; //书写二进制文件
ifile.close();
ofstream infile("count2,txt",ios::in|ios::out); /*更新x的值*/
infile<<x;
infile.close();
}
int compute::usechoice ()
{int a;
cout<<"输入要进行的操作:"<<endl;
cin>>a;
return a;
}
//输入住户信息
void compute::getdata()
{
int i,sno;
string tname;
char ttime[10];
int num,home;
string tel;
cout <<"输入总人数:";
cin>>ns;
for(i=0;i<ns;i++)
{
cout<<"输入编号:";
cin>>sno;
na[i].setNo (sno);
cout<<"输入姓名:";
cin>>tname;
na[i].setname(tname);
cout<<"输入电话号码:";
cin>>tel;
na[i].settelphone(tel);
cout<<"输入楼栋号:";
cin>>num;
na[i].setnumber(num);
cout<<"输入房间号:";
cin>>home;
na[i].sethomenum(home);
cout<<"输入落户时间:";
cin>>ttime;
na[i].settime(ttime);
}
}
//初始化界面
void compute::initdisp()
{
cout<<"***********************************************************"<<endl;
cout<<"** 小区物业管理系统 **"<<endl;
cout<<"***********************************************************"<<endl;
cout<<endl;
cout<<"1.输入住户信息"<<endl;
cout<<"2.添加住户信息"<<endl;
cout<<"3.查找住户信息"<<endl;
cout<<"4.修改住户信息"<<endl;
cout<<"5.读取住户信息"<<endl;
cout<<"6.删除住户信息"<<endl;
cout<<"7.恢复住户信息"<<endl;
cout<<"8.保存住户信息"<<endl;
cout<<"9.统计"<<endl;
cout<<"0.退出"<<endl;
}
//显示
void compute::disp()
{
cout<<"\t"<<"编号"<<"\t"<<"姓名"<<"\t"<<"电话号码"<<"\t"<<"楼栋号"<<"\t"<<"房间号"<<"\t"<<"落户时间"<<endl;
for( int i=0;i<ns;i++)
{
cout<<"\t"<<na[i].getNo ()<<"\t"<<na[i].getname()<<"\t"<<na[i].gettelphone()<<"\t"<<na[i].getnumber()<<"\t"<<na[i].gethomenum()<<"\t"<<na[i].gettime()<<endl;
}
}
//添加数据
void compute::adddate()
{
int i,sno;
string tname;
char ttime[10];
int num,home;
string tel;
if(ns==0)
{
cout<<"请选择1添加数据,然后在选择2增加记录"<<endl;
return ;
}
cout <<"输入要添加的人数:";
cin>>addns;
for(i=0;i<addns;i++)
{
cout<<"输入编号:";
cin>>sno;
na[ns+i].setNo(sno);
cout<<"输入姓名:";
cin>>tname;
na[ns+i].setname(tname);
cout<<"输入电话号码:";
cin>>tel;
na[ns+i].settelphone(tel);
cout<<"输入楼栋号:";
cin>>num;
na[ns+i].setnumber(num);
cout<<"输入房间号:";
cin>>home;
na[ns+i].sethomenum(home);
cout<<"输入落户时间:";
cin>>ttime;
na[ns+i].settime(ttime);
}
ns=ns+addns;
}
//按姓名查找
void compute::namefind(string cname)
{
int i;
int flag=0;
for(i=0;i<ns;i++)
{ if(na[i].getname()==cname)
{
dispone(i);
flag=1;
}
}
if(!flag)
{
cout<<"无此人!"<<endl;
return ;
}
}
void compute::dispone(int i)
{
cout<<"\t"<<"编号"<<"\t"<<"姓名"<<"\t"<<"电话号码"<<"\t"<<"楼栋号"<<"\t"<<"房间号"<<"\t"<<"落户时间"<<endl;
cout<<"\t"<<na[i].getNo ()<<"\t"<<na[i].getname()<<"\t"<<na[i].gettelphone()<<"\t"<<na[i].getnumber()<<"\t"<<na[i].gethomenum()<<"\t"<<na[i].gettime()<<endl;
}
string compute::inputonename ()
{
string cname;
cout<<"请输入要查找的住户姓名:";
cin>>cname;
return cname;
}
//删除记录
void compute::dele (string cname)
{
int cunt,i,x;
int flag=0;
for (i=0;i<ns;i++)
{
if(na[i].getname()==cname )
{
cunt=i;
flag=1;
x=i;
hf[x]=na[i];
}
}
if(!flag)
{
cout<<"输入错误,请重新输入!"<<endl;
return ;
}
for(i=cunt;i<ns-1;i++)
{
na[i].setname(na[i+1].getname());
na[i].settelphone (na[i+1].gettelphone ());
na[i].setnumber(na[i+1].getnumber());
na[i].sethomenum(na[i+1].gethomenum());
na[i].settime(na[i+1].gettime());
}
ns=ns-1;
}
//恢复函数
void compute::huifu(string c_name)
{
cout<<"恢复数据"<<endl;
int cunt,i,x;
int flag=0;
for (i=0;i<10;i++)
{
if(hf[x].getname()==c_name )
{
cunt=i;
flag=1;
i=x;
na[i]=hf[x];
}
}
if(!flag)
{
cout<<"输入错误,请重新输入!"<<endl;
return ;
}
for(i=cunt;i<ns+1;i++)
{
na[i].setname(na[i+1].getname());
na[i].settelphone (na[i+1].gettelphone ());
na[i].setnumber(na[i+1].getnumber());
na[i].sethomenum(na[i+1].gethomenum());
na[i].settime(na[i+1].gettime());
}
ns=ns+1;
}
//保存数据
void compute::save()
{
cout<<"保存数据"<<endl;
}
//
void compute::change()
{
cout<<"修改数据"<<endl;
}
//
void compute::tongji ()
{
cout <<"统计数据"<<endl;
}
//退出系统
void compute::quit ()
{
FLAG=0;
cout<<"程序即将关闭!"<<endl;
_sleep(1000);
_sleep(1000);
_sleep(1000);
return ;
}
//主函数
void main()
{ string nam;
compute obj;
int opt;
while(1)
{ obj.initdisp ();
opt=obj.usechoice ();
switch(opt)
{
case 1:
cout<<"==================================================="<<endl;
obj.getdata ();
cout<<"==================================================="<<endl;
break;
case 2:
cout<<"==================================================="<<endl;
obj.adddate ();
cout<<"==================================================="<<endl;
break;
case 3:
cout<<"==================================================="<<endl;
nam=obj.inputonename ();
obj.namefind (nam);
cout<<"==================================================="<<endl;
break;
case 4:
cout<<"==================================================="<<endl;
obj.change ();
cout<<"==================================================="<<endl;
break;
case 5:
cout<<"==================================================="<<endl;
obj.disp ();
cout<<"==================================================="<<endl;
break;
case 6:
cout<<"==================================================="<<endl;
nam=obj.inputonename ();
obj.dele (nam);
cout<<"==================================================="<<endl;
break;
case 7:
cout<<"==================================================="<<endl;
nam=obj.inputonename ();
obj.huifu (nam);
cout<<"==================================================="<<endl;
break;
case 8:
cout<<"==================================================="<<endl;
obj.save ();
cout<<"==================================================="<<endl;
break;
case 9:
cout<<"==================================================="<<endl;
obj.tongji ();
cout<<"==================================================="<<endl;
break;
case 0:
cout<<"==================================================="<<endl;
obj.quit();
cout<<"==================================================="<<endl;
break;
default :
cout<<"==================================="<<endl;
cout<<"选择错误,谢谢!"<<endl;
cout<<"==================================="<<endl;
}
}
}