C++ - Failing to parse CSV into my struct -
i have csv following format:
date,fruit,quantity1,quantity2,quantity3 2016-07-14,banana,3,20,6 2016-07-14,banana,3,50,15 2016-07-14,banana,0,25,15 2016-07-14,banana,3,25,6 2016-07-14,apple,3,10,20.5 2016-07-14,apple,0,30,5 2016-07-14,apple,0,5,30 2016-07-14,peach,3,10,30.2 2016-07-14,peach,3,40,4 2016-07-14,peach,3,10,12 2016-07-14,peach,0,10,8 2016-07-14,peach,3,200,3   i want parse file , store in struct. getting stack overflow error. failing exactly? because of clashing data types in struct? of data types float , i'm trying use getline , temporary string variable store info.
here complete code:
#include "stdafx.h" #include <iostream> #include <fstream> #include <sstream> #include <string>  using namespace std;  struct fruitinventory {     string date;     string fruit;     float quantity1;     float quantity2;     float quantity3; };  int main() {     ifstream myfile;     myfile.open("fruit_inventory.csv", ios::in);      string line;      fruitinventory todaysfruitsupply[15000];      int = 0;      int linecount = 0;      while (myfile.good())     {         getline(myfile, line);          stringstream mystream(line);          string temp;          if (i > 0) //ignore header line         {             getline(mystream, todaysfruitsupply[i].date, ',');             getline(mystream, todaysfruitsupply[i].fruit, ',');             getline(mystream, temp, ',');             todaysfruitsupply[i].quantity1 = stof(temp);             getline(mystream, temp, ',');             todaysfruitsupply[i].quantity2 = stof(temp);             getline(mystream, temp, ',');             todaysfruitsupply[i].quantity3 = stof(temp);         }          i++;         linecount++;     }      myfile.close();      system("pause");      return 0; }   edit: breaking on last line of file because there new-line character. after deleting that, executes completely. how can make sure can handle correctly in future?
this large object allocate local variable:
fruitinventory todaysfruitsupply[15000];   that's apparently cause of stack overflow. comment above says, should consider dynamic data structure such std::vector grow needed , manage memory automatically.
std::vector<fruitinventory> todaysfruitsupply;   it breaking on last line of file because there new-line character. after deleting that, executes completely. how can make sure can handle correctly in future?
you should check when read line it's not empty:
while (myfile.good()) {     getline(myfile, line);     if (line.empty())         break;   or better, don't use keep using good() instead test result of input operation:
while (getline(myfile, line) && !line.empty()) {   the whole thing like:
#include <iostream> #include <fstream> #include <sstream> #include <string>  using namespace std;  struct fruitinventory {     string date;     string fruit;     float quantity1;     float quantity2;     float quantity3; };  int main() {     ifstream myfile;     myfile.open("fruit_inventory.csv", ios::in);      string line;      std::vector<fruitinventory> todaysfruitsupply;      int linecount = 0;     getline(myfile, line); // ignore header line      fruitinventory inv;      while (getline(myfile, line) && !line.empty())     {         stringstream mystream(line);          string temp;         getline(mystream, inv.date, ',');         getline(mystream, inv.fruit, ',');         getline(mystream, temp, ',');         inv.quantity1 = stof(temp);         getline(mystream, temp, ',');         inv.quantity2 = stof(temp);         getline(mystream, temp, ',');         inv.quantity3 = stof(temp);         if (!mystream)             break; // went wrong reading line          todaysfruitsupply.push_back(inv);         linecount++;     } }      
Comments
Post a Comment