How to read from input file (text file) and validate input as valid integer?

前端 未结 2 394
执念已碎
执念已碎 2020-12-22 08:06

I\'m writing a program which needs to read a text file and check first line of the text file for a number between 0 to 10. I have come up with a couple of solutions but ther

2条回答
  •  渐次进展
    2020-12-22 08:50

    I would probably read the line with std::getline, then use a Boost lexical_cast to convert to int. It will throw an exception unless the entirety of the input string can be converted to the target type -- exactly what you want.

    Of course, you'll also need to check that the converted result is in the correct range, and also throw the exception if it's out of range.

提交回复
热议问题