1

I am new in C++ world and I`m trying to wrote an app which needs some properties (JSON file). I managed to parse the json and build the app. The problem is that I need to pun that json file in the same folder with the builded app so I can read it. How can I "inject" that JSON file in the build.

Now, I`m doing something like this to get the JSON file.

const char *Config::defaultConfigName()
{
    return "config.json";
}

I put that JSON in "Source File", in project. What path should I give in order to get that "config.json" from the project? Or what will be the best solution for this?

Thank you!

  • 1
    What operating system are you using? This Q&A shows [how to get executable path](https://stackoverflow.com/q/1528298/1270789), for instance, for both Windows and Linux, so you could place the file in that directory. – Ken Y-N Dec 26 '17 at 01:05
  • 1
    you can put the real JSON string (not file path) in source code – apple apple Dec 26 '17 at 06:04
  • can you please explain how to do that? I was not able to add the JSON as resource. – Giurca Adrian Dec 26 '17 at 22:27
  • @KenY-N I don`t want to to place the JSON file in a folder I want to embed it in the source code, somehow. – Giurca Adrian Dec 26 '17 at 22:32

1 Answers1

1

I managed to put the JSON text in source code:

const char *Config::defaultConfigName()
{

char const *json= R"config({
  "key": "value"
})config";

    return json;
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • I'd not seen this before, so for further information [here is another SO question](https://stackoverflow.com/q/30308088/1270789) and [here is a C++ reference](http://en.cppreference.com/w/cpp/language/string_literal), and [here is why you can't say `R"FOO( #include "config.json" )FOO"`](https://stackoverflow.com/q/37622767/1270789), which was my next question. – Ken Y-N Dec 27 '17 at 01:33