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中输入的内容 展开
#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中输入的内容 展开
展开全部
#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;
}
#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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#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;
}
#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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询