0

I have looked for answers on stackoverflow and none of them helped me so I am putting this question again. I have a config file config.json which looks like this :

{
    "key1" : "value1",
    "key2" : ["a", "b", "c"]
}

And I am trying this to read the config file :

#include <fstream>
#include <json/json.h>

std::ifstream config_file("config.json", std::ifstream::binary);
Json::Value config;
config_file >> config;
cout<<config;
cout<<config["key1"];

Running this gives me the following error:

Error from reader: * Line 1, Column 1
  Syntax error: value, object or array expected.
libc++abi.dylib: terminating with uncaught exception of type Json::RuntimeError: * Line 1, Column 1
  Syntax error: value, object or array expected.

Abort trap: 6

I can't seem to figure out what the issue is. Thank you for your time.

vectar
  • 61
  • 1
  • 2
  • 8
  • I'd guess `config_file` fails to open, you should check its state before passing it to jsoncpp – Alan Birtles Aug 23 '21 at 10:49
  • I'm not familiar with the library, but what do you expect the line `config_file >> config;` to do? – sweenish Aug 23 '21 at 10:50
  • @sweenish the code does work: https://godbolt.org/z/3aEvcYPh1 – Alan Birtles Aug 23 '21 at 10:52
  • @AlanBirtles you are right. The above code does work when the file is given as stringstream but doesn't work when I am trying to read the file. I guess the issue is with opening the file. – vectar Aug 23 '21 at 10:55
  • Thank you for your help @AlanBirtles . My program didn't have read access to the file. Resolved now. – vectar Aug 23 '21 at 11:05

0 Answers0