Reading data in from a .csv into usable format using C++ -


i able read data have c++ , start things manipulate it. quite new have tiny bit of basic knowledge. obvious way of doing strikes me (and maybe comes using excel previously) read data 2d array. code have far.

#include <iostream> #include <fstream> #include <algorithm> #include <string> #include <sstream>  using namespace std;  string c_j;  int main() {     float data[1000000][10];      ifstream c_j_input;     c_j_input.open("/users/rt/b/cj.csv");      if (!c_j_input) return -1;      for(int row = 0; row <1000000; row++)     {          string line;         getline(c_j_input, c_j, '?');         if ( !c_j_input.good() )             break;          stringstream iss(line);          for(int col = 0; col < 10; col++)             {             string val;             getline(iss, val, ',');             if (!iss.good() )                 break;              stringstream converter(val);             converter >> data[row][col];         }     }       cout << data;      return 0; } 

once have data read in able read through line line , pull analyse it, looking things think topic of thread, once have data read in.

just let me know if bad question in way , try add more might make better.

thanks!

as request of asker, how load string, split lines, , further split elements:

#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream>   //this takes string , splits delimiter , returns vector of strings std::vector<std::string> &splitstring(const std::string &s, char delim, std::vector<std::string> &elems) {     std::stringstream ss(s);     std::string item;     while (std::getline(ss, item, delim))     {         elems.push_back(item);     }     return elems; }   int main(int argc, char* argv[]) {      //load file ifstream     std::ifstream t("test.csv");     if (!t)     {         std::cout << "unknown file" << std::endl;         return 1;     }      //this block of code designed load whole file 1 string     std::string str;      //this sets read position end     t.seekg(0, std::ios::end);     str.reserve(t.tellg());//this gives string enough memory allocate the read position of file (which end)     t.seekg(0, std::ios::beg);//this sets read position beginning start reading      //this takes in stream (the file data) , loads string.     //istreambuf_iterator used loop through contents of stream (t), , in case go end.     str.assign((std::istreambuf_iterator<char>(t)),         std::istreambuf_iterator<char>());     //if (sizeof(rawdata) != *rawsize)     //  return false;      //if file has size (is not empty) analyze     if (str.length() > 0)     {         //the file loaded          //split delimeter(which newline character)         std::vector<std::string> lines;//this holds string each line in file         splitstring(str, '\n', lines);          //each element in vector holds vector of of elements(strings between commas)         std::vector<std::vector<std::string> > lineelements;            //for each line         (auto : lines)         {             //this vector of elements in line             std::vector<std::string> elementsinline;              //split comma, seperate "one,two,three" {"one","two","three"}             splitstring(it, ',', elementsinline);              //take elements in line, , add line-element vector             lineelements.push_back(elementsinline);         }          //this displays each element in organized fashion          //for each line         (auto : lineelements)         {             //for each element in line             (auto : it)             {                 //if not last element in line, insert comma                 if (i != it.back())                     std::cout << << ',';                 else                     std::cout << i;//last element not trailing comma             }             //the end of line             std::cout << '\n';         }     }     else     {         std::cout << "file empty" << std::endl;         return 1;     }      system("pause");     return 0; } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -