Search code examples
c++ifstream

Getting data from ifstream that failed conversion


I'm using an ifstream object to read double's from a text-file.

ifstreamObject >> floatVariable;

In the case that the read is not able to be converted into a double, I wondering how I can get the non-convertable data and make it into a string instead? Is it possible doing it this way without first storing it as a string and then try to convert it?

I want to do it this way so that the object throws an exception. The catch-block is intended to handle the values that are not convertable to double and store them in a separate txt-file for later analysis.


Solution

  • Use tellg to find current location and if conversion failed use seekg to go backwards and convert it to string.