converting string to int in C++

前端 未结 5 1576
南笙
南笙 2021-01-07 05:07

I am trying to convert a string I read in from a file to an int value so I can store it in an integer variable. This is what my code looks like:

ifstream sin         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-07 05:56

    The quick fix is to use line.c_str() which provides a const char* for atoi().

    A better solution (if available) may be to use boost::lexical_cast(line). This is a neater version of the C++ism of pushing things into and out of a std::stringstream which has all the type conversions you are likely to need.

提交回复
热议问题