0

I have one column of floats in a csv file. No column header.

string val;
vector<float> array;

string file = "C:/path/test.csv";

ifstream csv(file);
if (csv.is_open())
{
    string line;
    getline(csv, line);

    while (!csv.eof())
    {   
        getline(csv, val, '\n');
        array.push_back(stof(val));
    }

    csv.close();

}

I want to push the values in the column to vector array. When I use ',' as a delimiter it pushes the first line to the array but the rest of the column gets stuck together and unpushable. If I use '\n' it doesn't return the first line and I get a stof error.

I tried other answers unsuccessfully. What is the correct way to format this here?

test.csv

enter image description here

apgujeong
  • 99
  • 7
  • So you have a comma delimited line, and you want to access the float before the first comma? Have you tried reading the whole line, deleting everything from the first comma to the end of the line and then converting the remaining string to a float? – john Apr 24 '20 at 07:45
  • It might help to include a sample of your input file. What's confusing me is that if you really have one column of floats then why do you have any commas at all. – john Apr 24 '20 at 07:47
  • 2
    unrelated to your problem, but you should also read this: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – mch Apr 24 '20 at 07:48
  • Or this https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – john Apr 24 '20 at 07:49
  • The common way to process csv if to 1/ read the file line by line (delimiter `'\n'), and 2/ split each line on commas (for example with `strstream`). You can mix both but it will just be more complex... – Serge Ballesta Apr 24 '20 at 07:53
  • @john This was just a test csv file. I will include a sample. – apgujeong Apr 24 '20 at 07:54
  • What about [this answer](https://stackoverflow.com/a/1120224/5105949)? Seems pretty conclusive. If this doesn't help, can you give as an example of the csv file? – Lukas-T Apr 24 '20 at 07:54
  • Thanks for the replies. I will check the links. Am I using `eof()` wrong here or something? Should I be using while(getline... ? BTW this is a .cu file, although it shouldn't matter. – apgujeong Apr 24 '20 at 07:58
  • Yes, you should use `while(getline(...))`. Always check the if reading was successful before processing the data you read. – Lukas-T Apr 24 '20 at 08:11
  • Don't post an image of the csv file. Post the **text** of the csv file. It's the text that your program is reading. – john Apr 24 '20 at 08:18
  • well y'all were right, `while(getline(...)) did the trick. If one of you wants the credit just post an answer and ill accept. Otherwise Ill poist one myself. Thanks all! – apgujeong Apr 24 '20 at 08:19

1 Answers1

0

Your raw test.csv probably looks like this:

1.41286
1.425
1.49214
...

So there are no comma's, and using , as '(line) separator' would read the whole file and only parse the first float (up to the first \n).

Also, the first line is read but never used:

getline(csv, line);  // <- first line never used

while (!csv.eof())
{   
    getline(csv, val, '\n');
    array.push_back(stof(val));
}

Since there is only one field you don't have to use a separator and, as already mentioned in the comments, using while(getline(...)) is the right way to do this:

if (csv.is_open())
{
    string line;

    while (getline(..))
    {   
        array.push_back(stof(val));
    }

    csv.close();
}
Danny_ds
  • 11,201
  • 1
  • 24
  • 46