主题:[讨论]为什么会 “文件打开失败” ?
#include<string>
#include<fstream>
#include<iostream>
using namespace std;
class shuju
{
public:
setbill(int year,int month,int day,double spxf,double fzfy,double znjy,double sdfy,double ylfy,double qtzc,double cxje,double zcze,double shouru,double szce);
double setbill();
void display();
double show();
protected:
int year;
int month;
int day;
double spxf;
double fzfy;
double znjy;
double sdfy;
double ylfy;
double cxje;
double qtzc;
double shouru;
double zcze;
double szce;
};
shuju::setbill(int year,int month,int day,double spxf,double fzfy,double znjy,double sdfy,double ylfy,double qtzc,double cxje,double zcze,double shouru,double szce)
//年。月。日。食品消费。房租。子女教育。水电。医疗。储蓄。其它。支出总额。收入。收支差额。
{
year=year;
month=month;
day=day;
spxf=spxf;
fzfy=fzfy;
znjy=znjy;
sdfy=sdfy;
ylfy=ylfy;
cxje=cxje;
qtzc=qtzc;
shouru=shouru;
zcze=zcze;
szce=szce;
};
void shuju::display()
{
cout<<"====================================="<<endl;
cout<<"年"<<year<<endl;
cout<<"月"<<month<<endl;
cout<<"日"<<day<<endl;
cout<<"食品消费:"<<spxf<<endl;
cout<<"房租费用:"<<fzfy<<endl;
cout<<"子女教育:"<<znjy<<endl;
cout<<"水电费用:"<<sdfy<<endl;
cout<<"医疗费用:"<<ylfy<<endl;
cout<<"储蓄金额:"<<cxje<<endl;
cout<<"其他支出:"<<qtzc<<endl;
cout<<"收入:"<<shouru<<endl;
cout<<"支出总额:"<<zcze<<endl;
cout<<"收支差额:"<<szce<<endl;
};
double shuju::show()
{
ofstream outfile("mybill.txt",ios::out|ios::app);
if(!outfile)
{cerr<<"文件打开失败!"<<endl;
exit(1);
}
cout<<"请从键盘依次输入年、月、日,年月日之间以空格隔开,以回车结束..."<<endl;
cin>>year;
outfile<<year<<" ";
cin>>month;
outfile<<month<<" ";
cin>>day;
outfile<<day<<" ";
cout<<"请从键盘输入当月食品消费的金额,以回车结束..."<<endl;
cin>>spxf;
outfile<<spxf<<" ";
cout<<"请从键盘输入当月房租费用,以回车结束..."<<endl;
cin>>fzfy;
outfile<<fzfy<<" ";
cout<<"请从键盘输入当月子女教育的金额,以回车结束..."<<endl;
cin>>znjy;
outfile<<znjy<<" ";
cout<<"请从键盘输入当月水电费用,以回车结束..."<<endl;
cin>>sdfy;
outfile<<sdfy<<" ";
cout<<"请从键盘输入当月医疗费用,以回车结束..."<<endl;
cin>>ylfy;
outfile<<ylfy<<" ";
cout<<"请从键盘输入当月储蓄金额,以回车结束..."<<endl;
cin>>cxje;
outfile<<cxje<<" ";
cout<<"请从键盘输入当月其他支出金额,以回车结束..."<<endl;
cin>>qtzc;
outfile<<qtzc<<" ";
cout<<"请从键盘输入当月收入金额,以回车结束..."<<endl;
cin>>shouru;
outfile<<shouru<<" ";
zcze=spxf+fzfy+znjy+sdfy+ylfy+qtzc;
outfile<<zcze<<" ";
szce=shouru-zcze;
outfile<<szce<<" ";
return(zcze);
return(szce);
outfile.close();
};
int main()
{
int k;
shuju s[1000];
ifstream infile("f2.txt",ios::in);
if(!infile)
{cerr<<"文件打开失败!"<<endl;
exit(1);
}
infile>>k;
ofstream outfile("f2.txt",ios::out|ios::trunc);
if(!outfile)
{cerr<<"文件打开失败!"<<endl;
exit(1);
}
s[k].show();
s[k].display();
k=k+1;
outfile<<k;
outfile.close();
return 0;
}
#include<fstream>
#include<iostream>
using namespace std;
class shuju
{
public:
setbill(int year,int month,int day,double spxf,double fzfy,double znjy,double sdfy,double ylfy,double qtzc,double cxje,double zcze,double shouru,double szce);
double setbill();
void display();
double show();
protected:
int year;
int month;
int day;
double spxf;
double fzfy;
double znjy;
double sdfy;
double ylfy;
double cxje;
double qtzc;
double shouru;
double zcze;
double szce;
};
shuju::setbill(int year,int month,int day,double spxf,double fzfy,double znjy,double sdfy,double ylfy,double qtzc,double cxje,double zcze,double shouru,double szce)
//年。月。日。食品消费。房租。子女教育。水电。医疗。储蓄。其它。支出总额。收入。收支差额。
{
year=year;
month=month;
day=day;
spxf=spxf;
fzfy=fzfy;
znjy=znjy;
sdfy=sdfy;
ylfy=ylfy;
cxje=cxje;
qtzc=qtzc;
shouru=shouru;
zcze=zcze;
szce=szce;
};
void shuju::display()
{
cout<<"====================================="<<endl;
cout<<"年"<<year<<endl;
cout<<"月"<<month<<endl;
cout<<"日"<<day<<endl;
cout<<"食品消费:"<<spxf<<endl;
cout<<"房租费用:"<<fzfy<<endl;
cout<<"子女教育:"<<znjy<<endl;
cout<<"水电费用:"<<sdfy<<endl;
cout<<"医疗费用:"<<ylfy<<endl;
cout<<"储蓄金额:"<<cxje<<endl;
cout<<"其他支出:"<<qtzc<<endl;
cout<<"收入:"<<shouru<<endl;
cout<<"支出总额:"<<zcze<<endl;
cout<<"收支差额:"<<szce<<endl;
};
double shuju::show()
{
ofstream outfile("mybill.txt",ios::out|ios::app);
if(!outfile)
{cerr<<"文件打开失败!"<<endl;
exit(1);
}
cout<<"请从键盘依次输入年、月、日,年月日之间以空格隔开,以回车结束..."<<endl;
cin>>year;
outfile<<year<<" ";
cin>>month;
outfile<<month<<" ";
cin>>day;
outfile<<day<<" ";
cout<<"请从键盘输入当月食品消费的金额,以回车结束..."<<endl;
cin>>spxf;
outfile<<spxf<<" ";
cout<<"请从键盘输入当月房租费用,以回车结束..."<<endl;
cin>>fzfy;
outfile<<fzfy<<" ";
cout<<"请从键盘输入当月子女教育的金额,以回车结束..."<<endl;
cin>>znjy;
outfile<<znjy<<" ";
cout<<"请从键盘输入当月水电费用,以回车结束..."<<endl;
cin>>sdfy;
outfile<<sdfy<<" ";
cout<<"请从键盘输入当月医疗费用,以回车结束..."<<endl;
cin>>ylfy;
outfile<<ylfy<<" ";
cout<<"请从键盘输入当月储蓄金额,以回车结束..."<<endl;
cin>>cxje;
outfile<<cxje<<" ";
cout<<"请从键盘输入当月其他支出金额,以回车结束..."<<endl;
cin>>qtzc;
outfile<<qtzc<<" ";
cout<<"请从键盘输入当月收入金额,以回车结束..."<<endl;
cin>>shouru;
outfile<<shouru<<" ";
zcze=spxf+fzfy+znjy+sdfy+ylfy+qtzc;
outfile<<zcze<<" ";
szce=shouru-zcze;
outfile<<szce<<" ";
return(zcze);
return(szce);
outfile.close();
};
int main()
{
int k;
shuju s[1000];
ifstream infile("f2.txt",ios::in);
if(!infile)
{cerr<<"文件打开失败!"<<endl;
exit(1);
}
infile>>k;
ofstream outfile("f2.txt",ios::out|ios::trunc);
if(!outfile)
{cerr<<"文件打开失败!"<<endl;
exit(1);
}
s[k].show();
s[k].display();
k=k+1;
outfile<<k;
outfile.close();
return 0;
}