Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

c++ - Reading json text file fails parsing. Is ostringstream doing something to it?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the error thanks to the comments of the users.

The file was saved in an Utf-8 encoding with BOM, adding symbols not printed in console, magic numbers that tell the editor that the document is encoded as utf-8.

To print hidden symbols in Linux:

cat filename.json -A

You can save the file without BOM or change the encoding of the file to ascii in case you don't want utf-8:

iconv -c -f utf-8 -t ascii -o out.txt in.txt

(from utf-8 to ascii, -c ignores invalid characters)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...