c++ - Assigning Comma Delimited Data to Vector -


i have comma delimited data in file, so:

116,88,0,44 66,45,11,33

etc. know size, , want each line own object in vector.

here implementation:

bool addobjects(string filename) {  ifstream inputfile;  inputfile.open(filename.c_str());  string fileline; stringstream stream1; int element = 0;  if (!inputfile.is_open()) {     return false; }  while(inputfile) {         getline(inputfile, fileline); //get line file         movingobj(fileline); //use external class parse data comma         stream1 << fileline; //assign string stringstream         stream1 >> element; //turn string object vector         movingobjects.push_back(element); //add object vector     }    inputfile.close(); return true; 

}

no luck far. errors in

stream1 << fileline

and push_back statement. stream1 1 tells me there's no match << operator (there should be; included library) , latter tells me movingobjects undeclared, seemingly thinking function, when defined in header vector.

can offer assistance here? appreciated!

if understood correctly intentions should close you're trying do:

#include <algorithm> #include <fstream> #include <iostream> #include <string> #include <sstream> #include <vector>  void movingobj(std::string & fileline) {     replace(fileline.begin(), fileline.end(), ',', ' '); }  bool addobjects(std::string filename, std::vector<int> & movingobjects) {     std::ifstream inputfile;     inputfile.open(filename);      std::string fileline;     int element = 0;     if (!inputfile.is_open()) {         return false;     }     while (getline(inputfile, fileline)) {         movingobj(fileline); //use external class parse data comma         std::stringstream stream1(fileline); //assign string stringstream         while ( stream1 >> element ) {             movingobjects.push_back(element); //add object vector         }     }     inputfile.close();     return true; }  int main() {     std::vector<int> movingobjects;     std::string filename = "data.txt";     addobjects(filename, movingobjects);     ( int : movingobjects ) {         std::cout << << std::endl;     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -