猿问

比如有个文本文档叫data.txt有以下的数据:请问该怎么读入?

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++!!谢谢

慕斯709654
浏览 164回答 2
2回答

斯蒂芬大帝

测通试过,请过目:#include<iostream>#include<fstream>#include<vector>usingnamespacestd;structinfo{stringman;stringdes;doublepri;};intmain(){ifstreamin("in.txt");vector<info>v;infoi;charbuffer[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_ti=0;i<v.size();++i)cout<<v[i].man<<"\t\t"<<v[i].des<<"\t\t"<<v[i].pri<<'\n';return0;}

梦里花落0921

#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;}
随时随地看视频慕课网APP
我要回答