1

I'm trying to read in a .csv file filled with floats. I used this to build my code. However, the data reads in correctly but is read in as a string but I want to use the data as floats. If I try to use stof(string) I get an error that it is trying to convert a non number to a number. So I went the really long way and converted the string to a char and that to a float, which works but is VERY ugly. However, once all the data is read in and is printed out with a cout the program my crashes

trackBarFile.open("test2.csv");


std::string line, line2, line3;
int count;
std::string token;
float tokenNum,lineFloat,line2Float,line3Float;
char cstr[5],cstr2[5];

while (getline(trackBarFile, line,',')) 
{

    cstr[line.size()+1];
    strcpy(cstr, line.c_str());
    lineFloat = atof(cstr);

    getline(trackBarFile, line2,',');
    cstr[line2.size()+1];
    strcpy(cstr, line2.c_str());
    line2Float = atof(cstr);

    getline(trackBarFile, line3);       
    cstr2[line3.size()+1];
    strcpy(cstr2, line3.c_str());
    line3Float = atof(cstr2);


    std::cout<<line<<","<<lineFloat<<"   , "<<line2<<","<<line2Float<<"  ,  "<<line3<<","<<line3Float<<std::endl;


}

trackBarFile.close();
TomEcho
  • 31
  • 1
  • 7
  • 2
    What do you hope to accomplish by the lines of the form `cstr[line.size()+1];`? – R Sahu Jan 09 '19 at 19:41
  • 1
    related/a really good read: https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c – NathanOliver Jan 09 '19 at 19:41
  • I would start by getting rid of the char arrays and strcpy – drescherjm Jan 09 '19 at 19:42
  • What do you seek to accomplish by having calls to `getline()` inside the loop `while ( getline() )`? – Codes with Hammer Jan 09 '19 at 19:46
  • the cstr[] part changes the length of the char, from my understanding. The char array and strcpy are there because it would not convert the string to a float. – TomEcho Jan 09 '19 at 19:46
  • the getline() inside the while is what checks to see if the loop continues or not. I got it from the link I attached in my description – TomEcho Jan 09 '19 at 19:48
  • @CodeswithHammer That is one of the things the asker has right in this post. [Good explanation of what it's dong and how here](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – user4581301 Jan 09 '19 at 19:48
  • Tom, once an array is created its size cannot be changed. `cstr[line.size()+1];` accesses an element of the array (and probably doesn't exist--Kaboom!) and does nothing with it. Odds are good that the compiler will optimize the line out. I recommend using `std::string` throughout. It is smart enough to change size when used correctly. – user4581301 Jan 09 '19 at 19:51
  • @user4581301: Ah, it's been a while since I've worked with getline() and raw handling of CSV files. – Codes with Hammer Jan 09 '19 at 19:52
  • @user4581301 Can you explain more how I would go about using std::string throughout? If I remove the cstr, strcpy and atof lines and only leave the 3 getline()s the cout out prints correctly however once I attempt to convert the strings to floats the program crashes with no errors – TomEcho Jan 09 '19 at 20:10

1 Answers1

1

It seems I have stumbled upon the answer to my own question. Thanks to the above questions I started looking for different ways to convert the string to a float. The +2 in the print out can be ignored, was my "pinch" to make sure I wasn't dreaming

trackBarFile.open("TrackBarSignal.csv");

std::ofstream fout;
fout.open("Output_ReadInCSV.txt");

std::string line, line2, line3;
int count;
float tokenNum,lineFloat,line2Float,line3Float;

while (getline(trackBarFile, line,',')&&getline(trackBarFile, line2,',')&&getline(trackBarFile, line3)) 
{

    lineFloat = (float)atof(line.c_str());
    line2Float = (float)atof(line2.c_str());
    line3Float = (float)atof(line3.c_str());

    std::cout<<line<<","<<lineFloat+2<<"   ,   "<<line2<<","<<line2Float+2<<"  ,  "<<line3<<","<<line3Float+2<<std::endl;

}

trackBarFile.close();
TomEcho
  • 31
  • 1
  • 7
  • Recommendations: `while (getline(trackBarFile, line,',') && getline(trackBarFile, line2,',') && getline(trackBarFile, line3))` You need to have successfully read all three entries to be able to process all three readings. `atof` does't tell you if it failed to convert the string, it just returns 0.0, so consult documentation for the `std::stof` function which throws an exception on failure. In addition `std::stof` directly accepts `std::string` and directly returns a `float`, so `lineFloat = std::stof(line);` – user4581301 Jan 09 '19 at 20:29
  • Also try to avoid stumbling. It's a really slow way to learn. C++ is a very complex language, probably one of the hardest to learn of all languages in general use today. Trying to learn it by surfing the web for examples or asking questions on Stack Overflow is bordering on futile. You're better served by getting and working through a [set of good reference books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Jan 09 '19 at 20:38
  • I edited the above answer to reflect your recommendation after testing it. I first tried to use stof() but it throws the exception. I've gone through and compared the line and lineFloat data and can not see any points that it has converted incorrectly. I found that if I use the stof function just below the cout, the cout for what should be "0" would print out "0@@0", but if I now commented out the convert line the print out now read "0" – TomEcho Jan 09 '19 at 20:57