Reputation: 46
I want to read a txt file and convert two cells from each line to floats.
If I first run:
someString = someString.substr(1, tempLine.size());
And then:
std::stof(someString)
it only converts the first number in 'someString' to a number. The rest of the string is lost.
When I handled the string in my IDE I noticed that copying it and pasting it inside quotation marks gives me "\u00005\u00007\u0000.\u00007\u00001\u00007\u00007\u0000"
and not 57.7177
.
If I instead do:
std::string someOtherString = "57.7177"
std::stof(someOtherString)
I get 57.7177
.
Minimal working example is:
int main() {
std::string someString = "\u00005\u00007\u0000.\u00007\u00001\u00007\u00007\u0000";
float someFloat = std::stof(someString);
return 0;
}
Same problem occurs using both UTF-8 and -16 encoding.
What is happening and what should I do differently? Should I remove the null-characters somehow?
Upvotes: 0
Views: 848
Reputation: 66922
"I want to read a txt file"
What is the encoding of the text file? "Text" is not a encoding. What I suspect is happening is that you wrote code that reads in the file as either UTF8 or Windows-1250 encoding, and stored it in a std::string
. From the bytes, I can see that the file is actually UTF16BE, and so you need to read into a std::u16string
. If your program will only ever run on Windows, then you can get by with a std::wstring
.
You probably have followup questions, but your original question is vague enough that I can't predict what those questions would be.
Upvotes: 3