2 回答
TA贡献1827条经验 获得超8个赞
测通试过,请过目:
#include
<iostream>
#include
<fstream>
#include
<vector>
using
namespace
std;
struct
info
{
string
man;
string
des;
double
pri;
};
int
main()
{
ifstream
in("in.txt");
vector<info>
v;
info
i;
char
buffer[256];
while(in)
{
in.getline(buffer,
256,
';');
i.man
=
buffer;
in.getline(buffer,
256,
';');
i.des
=
buffer;
in
>>
i.pri;
in.ignore(8,
'\n');
v.push_back(i);
}
in.close();
cout
<<
"Manufacturer\tDescription\tPrice\n";
cout.setf(ios::fixed,ios::floatfield);
cout.precision(3);
for(size_t
i
=
0;
i
<
v.size();
++i)
cout
<<
v[i].man
<<
"\t\t"
<<
v[i].des
<<
"\t\t"
<<
v[i].pri
<<
'\n';
return
0;
}
TA贡献1772条经验 获得超6个赞
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct tmp{
char a[20];
char b[20];
double data;
};
tmp MArray[6];
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile("data.txt",ios::binary);
if(!infile.is_open()){
cerr<<"error!\n";
return 0;
}
int i=0;
while(!infile.eof()){
infile.get(MArray[i].a,20,';');
infile.get(); //跳过';'
infile.get(MArray[i].b,20,';');
infile.get(); //跳过';'
infile>>MArray[i].data;
infile.ignore(10,'\n');//读到这一行的'\n'是停止
infile.get(); //换到下一行
i++;
}
for(int i=0;i<6;i++)
cout <<MArray[i].a<<" "<<MArray[i].b<<" "<<MArray[i].data<<endl;
return 0;
}
添加回答
举报