How to work with NaN when reading in a CSV file in C ++

I am reading in a CSV time series data file into a C ++ program. However, my data contains some NaN. For instance:

1-Jul-2010,   1.0 
2-Jul-2010,   2.0
3-Jul-2010,   NaN
4-Jul-2010,   3.0

To handle this, I wrote a short script in Matlab that replaces all NaN with 0.0 - Then I read in a new file without NaN. Is there an easy way or to avoid this preprocessing?

Thank!

+3
source share
2 answers

As David Given mentioned earlier, you don’t need to pre-process the file. strtof () and strtod () can convert a NaN string to a float / double NaN value.

If you want to replace the values ​​with 0.0 in your dataset, you can do this using the isnan () function.

if (isnan(val))
{
    val = 0.0;
}
+6

, ( ) NaN - stdlib strtod().

- ( 0 ..), , NaN strcmp(), , - , NaN .

0

Source: https://habr.com/ru/post/1758759/


All Articles