I have a C++ programming question that I'm asked to read from a file that contains several floats, words and symbols (e.g. # ! %
). From this file I must take only the floats and store it into an array.
The text file may look like this
11 hello 1.00 16.0 1.999
I know how to open the file; it's just grabbing ONLY the floats I'm struggling with.
As long as you don't mind treating integral numbers as floats, such as
11
in your post, you can use the following strategy.Something along the lines of the code below should work.
I'd use a ctype facet that classifies everything except for digits as being white space:
Then imbue the stream with a locale using that facet, and just read your numbers:
Result:
You have to use
fscanf()
. Read the documentation for information how to use it.