Is it possible to read infinity or NaN values using input streams?

后端 未结 6 1039
北荒
北荒 2021-01-01 08:45

I have some input to be read by a input filestream (for example):

-365.269511 -0.356123 -Inf 0.000000

When I use std::ifstream mystream;

6条回答
  •  再見小時候
    2021-01-01 09:01

    Just read your variables into string and parse them. You can't put string into double variables and expect them to be outputted like a string, because if it worked, strings wouldn't be necessary.

    Seomthing like:

    string text;
    double d;
    while(cin >> text)
    {
        if(text == "Inf")       //you could also add it with negative infinity
        {
             d = std::numeric_limits::infinity();
        }
        else
        {
            d = atof(text.c_str());
        }
    }
    

提交回复
热议问题