1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<tuple>
#include<iomanip>
using namespace std;
int main (){
fstream File;
vector<tuple<string,double,string,double>> v;//container to store the data (C++11 syntax);
File.open("F:\\test.txt");
if(File.is_open()){//doesn't error check file non-opening, something for OP to consider;
string line;
static const auto delimiter = ','; //C++11 syntax;
while(getline (File, line)){//reads File into the string line;
stringstream stream(line);//constructs the stringstream object, stream, with the string line;
string co_name, item_name;
double item_qty, item_px;
getline(stream, co_name, delimiter) && // reads stream into co_name upto, but not including, delimiter;
stream>> item_qty >> item_name >> item_px; // reads rest of the stream into item_qty, item_name, item_px;
v.emplace_back(co_name, item_qty, item_name, item_px);//fills vector<tuple<>> with the data;
}
}
File.close();
for(auto& itr : v){//using range-loop (C++11);
get<0>(itr) += ":"; //adds the colon to each co_name;
cout<<setw(20)<<left<<get<0>(itr)<<get<1>(itr)<<" "<<get<2>(itr)<<" @ "<<get<3>(itr)<<"\n";
//prints the vector element by element;
}
}
|