c++ 文本文档中读入分号

来源:百度知道 编辑:UC知道 时间:2024/05/28 14:12:38
Laptop;Canon;6399.99
Laptop;Kodak;4399.99
Laptop;Sony;7399.99
Laptop;IBM;2399.99
Laptop;Toshiba;6499.99
Laptop;HP;3299.999
比如有个文本文档叫data.txt有以下的数据:
Laptop;Canon;499.99
Laptop;Kodak;299.99
Laptop;Sony;399.99
Laptop;IBM;899.99
Laptop;Toshiba;1499.99
Laptop;HP;1299.999
怎么读入?C++!!谢谢

跟分号有什么关系? 正常读取啊 ,楼主想要说什么?

#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