c++getline读取文件
求高手解答读取文件DATA.TXT,文件内容为Code,Itemname,Description,Price(ExGST),GST0001,Fishhook,Useful...
求高手解答
读取文件DATA.TXT,
文件内容为
Code, Item name, Description, Price (Ex GST), GST
0001, Fish hook, Useful for catching fish, 0.5, 1
0110, Metronome, A device for keeping musical time, 49, 1
1234, Basil, Bunch of basil, 2.5, 0
0002, Bread, Essene Sprouted Bread, 5.2, 0
1000, Blah, Not something you'd buy, 12.4, 1
我做的是
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct{
string id;
string name;
string dec;
double price;
double gst;
}abc;
int main()
{
abc s[6];
int i;
ifstream fin;
fin.open("data.txt");
if(fin.is_open()==false)cout<<"error"<<endl;
else
fin.ignore(100,'\n');
for(i=1;i<=6;i++){
getline(fin,s[i].id,',');
getline(fin,s[i].name,',');
getline(fin,s[i].dec,',');
fin>>s[i].price;
fin>>s[i].gst;
cout<<s[i].id<<" - "<<s[i].name<<"("<<s[i].dec<<"): $"<<s[i].price<<endl;
};
fin.close();
return 0;
}
只COUT了第一行,我知道是光标错误,可是自己不会改,求高手帮忙~~
显示成这个样子
0001 - Fish hook (Useful for catching fish): $0.50*
0110 - Metronome (A device for keeping musical time): $49.00*
1234 - Basil (Bunch of basil): $2.50
。。。
谢谢~ 展开
读取文件DATA.TXT,
文件内容为
Code, Item name, Description, Price (Ex GST), GST
0001, Fish hook, Useful for catching fish, 0.5, 1
0110, Metronome, A device for keeping musical time, 49, 1
1234, Basil, Bunch of basil, 2.5, 0
0002, Bread, Essene Sprouted Bread, 5.2, 0
1000, Blah, Not something you'd buy, 12.4, 1
我做的是
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct{
string id;
string name;
string dec;
double price;
double gst;
}abc;
int main()
{
abc s[6];
int i;
ifstream fin;
fin.open("data.txt");
if(fin.is_open()==false)cout<<"error"<<endl;
else
fin.ignore(100,'\n');
for(i=1;i<=6;i++){
getline(fin,s[i].id,',');
getline(fin,s[i].name,',');
getline(fin,s[i].dec,',');
fin>>s[i].price;
fin>>s[i].gst;
cout<<s[i].id<<" - "<<s[i].name<<"("<<s[i].dec<<"): $"<<s[i].price<<endl;
};
fin.close();
return 0;
}
只COUT了第一行,我知道是光标错误,可是自己不会改,求高手帮忙~~
显示成这个样子
0001 - Fish hook (Useful for catching fish): $0.50*
0110 - Metronome (A device for keeping musical time): $49.00*
1234 - Basil (Bunch of basil): $2.50
。。。
谢谢~ 展开
1个回答
展开全部
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct{
string id;
string name;
string dec;
double price;
double gst;
}abc;
int main()
{
abc s[5];
int i;
ifstream fin;
fin.open("data.txt");
if(fin.is_open()==false)cout<<"error"<<endl;
else
fin.ignore(100,'\n');
for(i=0;i<5;i++){
getline(fin,s[i].id,',');
getline(fin,s[i].name,',');
getline(fin,s[i].dec,',');
fin>>s[i].price;
fin.ignore(1,'\n');
fin>>s[i].gst;
fin.ignore(100,'\n');
cout<<s[i].id<<" - "<<s[i].name<<"("<<s[i].dec<<"): $"<<s[i].price<<endl;
};
fin.close();
return 0;
}
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct{
string id;
string name;
string dec;
double price;
double gst;
}abc;
int main()
{
abc s[5];
int i;
ifstream fin;
fin.open("data.txt");
if(fin.is_open()==false)cout<<"error"<<endl;
else
fin.ignore(100,'\n');
for(i=0;i<5;i++){
getline(fin,s[i].id,',');
getline(fin,s[i].name,',');
getline(fin,s[i].dec,',');
fin>>s[i].price;
fin.ignore(1,'\n');
fin>>s[i].gst;
fin.ignore(100,'\n');
cout<<s[i].id<<" - "<<s[i].name<<"("<<s[i].dec<<"): $"<<s[i].price<<endl;
};
fin.close();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询