C++从文件中读取数据再输出

#include<iostream>#include<fstream>usingnamespacestd;#include<string>classEquipment{p... #include<iostream>
#include<fstream>
using namespace std;
#include<string>
class Equipment
{
public:
void getin();
string name;
string factory;
int date;
int count;
int value;
};
Equipment equip[100];
int quanity;
int i;void Equipment::getin()
{
cin>>num>>name>>factory>>date>>count>>value;
}
void set_data()
{
ofstream outfile("equip.txt",ios::out);
if(!outfile)
{
cerr<<"open equip.txt error!"<<endl;
exit(1);
}
cout<<"please input the number of devices"<<endl;
cin>>quanity;
cout<<" please input the informations in order: Number,Name,Factory,Count,Value"<<endl;
for(i=0;i<quanity;i++)
{
equip[i].getin();
outfile<<equip[i].num<<' '<<equip[i].name<<' '<<equip[i].factory<<' '<<equip[i].date<<' '<<equip[i].count<<' '<<equip[i].value<<endl;
}
outfile<<quanity<<endl;
outfile.close();
}
void read_data()
{
ifstream infile("equip.txt",ios::in);
if(!infile)
{
cerr<<"open equip.txt error!"<<endl;
exit(1);
}
infile>>quanity;
for(i=0;i<quanity;i++)
{
infile>>equip[i].num>>equip[i].name>>equip[i].factory>>equip[i].count>>equip[i].value;
cout<<equip[i].num<<' '<<equip[i].name<<' '<<equip[i].factory<<' '<<equip[i].date<<' '<<equip[i].count<<' '<<equip[i].value<<endl;
}
infile.close();
}
void main()
{
set_data();
read_data();
}
程序如上,不知道为什么运行后read_data函数不能正确在显示器上输出set_data中输入的内容
展开
 我来答
feiyu5217
2015-10-22 · TA获得超过11.3万个赞
知道大有可为答主
回答量:9692
采纳率:71%
帮助的人:3588万
展开全部
#include <stdlib.h>
#include<iostream>
#include<fstream>
using namespace std;
#include<string>
class Equipment
{
public:
void getin();
int num;
string name;
string factory;
int date;
int count;
int value;
};
Equipment equip[100];
int quanity;
int i;
void Equipment::getin()
{
cin>>num>>name>>factory>>date>>count>>value;
}
void set_data()
{
ofstream outfile("equip.txt",ios::out);
if(!outfile)
{
cerr<<"open equip.txt error!"<<endl;
exit(1);
}
cout<<"please input the number of devices"<<endl;
cin>>quanity;
outfile<<quanity<<endl;
for(i=0;i<quanity;i++)
{
cout<<" please input the "<<i <<" informations in order: Number,Name,Factory,date, Count,Value"<<endl;
equip[i].getin();
outfile<<equip[i].num<<' '<<equip[i].name<<' '<<equip[i].factory<<' '<<equip[i].date<<' '<<equip[i].count<<' '<<equip[i].value<<endl;
}
outfile.close();
}
void read_data()
{
ifstream infile("equip.txt",ios::in);
if(!infile)
{
cerr<<"open equip.txt error!"<<endl;
exit(1);
}
infile>>quanity;
for(i=0;i<quanity;i++)
{
infile> >equip[i].num>>equip[i].name>>equip[i].factory>>equip[i].count>>equip[i].value;
cout<<equip[i].num<<' '<<equip[i].name<<' '<<equip[i].factory<<' '<<equip[i].date<<' '<<equip[i].count<<' '<<equip[i].value<<endl;
}
infile.close();
}
int main()
{
set_data();
read_data();
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
chr1999
2013-06-28 · TA获得超过1056个赞
知道小有建树答主
回答量:2462
采纳率:0%
帮助的人:1244万
展开全部
#include <stdlib.h>
#include<iostream>
#include<fstream>
using namespace std;
#include<string>
class Equipment
{
public:
void getin();
int num;
string name;
string factory;
int date;
int count;
int value;
};
Equipment equip[100];
int quanity;
int i;
void Equipment::getin()
{
cin>>num>>name>>factory>>date>>count>>value;
}
void set_data()
{
ofstream outfile("equip.txt",ios::out);
if(!outfile)
{
cerr<<"open equip.txt error!"<<endl;
exit(1);
}
cout<<"please input the number of devices"<<endl;
cin>>quanity;
outfile<<quanity<<endl;
for(i=0;i<quanity;i++)
{
cout<<" please input the "<<i <<" informations in order: Number,Name,Factory,date, Count,Value"<<endl;
equip[i].getin();
outfile<<equip[i].num<<' '<<equip[i].name<<' '<<equip[i].factory<<' '<<equip[i].date<<' '<<equip[i].count<<' '<<equip[i].value<<endl;
}
outfile.close();
}
void read_data()
{
ifstream infile("equip.txt",ios::in);
if(!infile)
{
cerr<<"open equip.txt error!"<<endl;
exit(1);
}
infile>>quanity;
for(i=0;i<quanity;i++)
{
infile>>equip[i].num>>equip[i].name>>equip[i].factory>>equip[i].count>>equip[i].value;
cout<<equip[i].num<<' '<<equip[i].name<<' '<<equip[i].factory<<' '<<equip[i].date<<' '<<equip[i].count<<' '<<equip[i].value<<endl;
}
infile.close();
}
int main()
{
set_data();
read_data();
return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式