I read a text file (.json) that contains this:
{ "a" : true }
I read the file using:
std::ifstream fileStream;
fileStream.open(configFilePath,std::ios::in);
std::ostringstream sstr;
sstr << fileStream.rdbuf();
std::string text = sstr.str();
fileStream.close();
If I print the contents, I get the correct text in the output.
The json parser I have (taken from here: https://github.com/sheredom/json.h) fails to try to parse the file.
If I set the string by hand:
std::string text = "{"a" : true }";
The parser works.
What is ostringstream + ifstream doing to the format or contents of the file to make the parsing fail?
[EDIT]
As a user suggests I compared both strings and they were different.
When printing with "cat -A" I get all the hidden characters.
The first one is the file contents. Second is the string created by hand in the code:
File opened. Reading...$
M-oM-;M-?{"a":true}^M$
$
{"a":true}$
The file contains 15 characters
The text contains just 10.
To print the first line I use:
_logFile << "File opened. Reading..." << std::endl << std::flush;
So my question is... Why are ifstream + ofstringstream adding those?
question from:
https://stackoverflow.com/questions/65885754/reading-json-text-file-fails-parsing-is-ostringstream-doing-something-to-it 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…