Inserting csv info into a vector
I need help populating my vector with the 3 existing entries. I have unsuccessful in finding a video or guide in doing so. Thank you for your help!
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 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
struct employee
{
string ID;
string Fame;
string Lname;
};
vector<employee> sVec(3);
ifstream inFile("indata.csv");
ofstream outFile("outdata.csv");
if(!inFile.is_open())
{
cout << "Error File Open" << '\n';
}
if(!outFile.is_open())
{
cout << "Error File Write" << '\n';
}
string record;
string firstname;
string lastname;
string ID;
string email;
string exam1;
string exam2;
string exam3;
int i = 1;
while(!inFile.eof())
{
getline(inFile,ID,',');
getline(inFile,firstname,',');
getline(inFile,lastname,',');
getline(inFile,ID,',');
if( inFile.eof() ) break;
}
|
Try this
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
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
struct employee
{
string ID;
string Fame;
string Lname;
};
int main ( ) {
ifstream inFile("foo.csv");
string line;
while ( getline(inFile,line) ) {
istringstream is(line);
employee temp;
getline(is,temp.ID,',');
getline(is,temp.Fame,',');
getline(is,temp.Lname,',');
cout << temp.ID << "," << temp.Fame << "," << temp.Lname << endl;
}
}
|
Topic archived. No new replies allowed.