How to add json file to compile with everything else Visual Studio 2019 and 2022

John Le Roux 96 Reputation points
2021-10-14T07:53:26.737+00:00

Hello, I am having trouble using Visual Studio Community 2019 and 2022.

In my code I am trying to read and write a json file but I keep getting parse errors because the json file is not included with the exe when compiling? Here is my code.

How can I fix this? I thought the problem was with the 3rd party json software I use but it is not, it does have something to do with compiling the code.

Thank you!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,519 questions
0 comments No comments
{count} votes

Accepted answer
  1. John Le Roux 96 Reputation points
    2021-10-16T07:26:58.537+00:00

    Alright, here is the answer. At first I was having problems with fstream not reading my json file etc. so I thought the problem was that the json file was not added to the project and so fstream couldn't read the file, but I was wrong.

    The real question was what do I put as the relative path to my json file? at the root of my project I have the Debug/ folder with the exe, and at the root I have a folder called data/ that has the file I want fstream to read. I thought the relative path to the json was to go ../data/ my json file but that was not the correct answer, what is the correct answer was to just do data/ my json file because the exe's relative path starts at the root of the project.

    Tl;dr: If you are using fstream and vs community, the relative path starts at the root of the project, most likely if you build x86, just behind the Debug/ folder.

    I hope I make sense, and thanks for your help!

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Minxin Yu 9,866 Reputation points Microsoft Vendor
    2021-10-15T06:20:11.747+00:00

    Hi, @John Le Roux

    I tested the sample in my PC, changed the \ to / in file.cpp, and it now works.
    140739-%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE-2021-10-15-141020.png
    140698-capture.png

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. John Le Roux 96 Reputation points
    2021-10-15T12:26:50.267+00:00

    It works now, but what do I put as the relative path to the json file if I don't want to use the actual path? @Minxin Yu

    0 comments No comments

  3. SM 416 Reputation points
    2021-10-15T19:38:11.34+00:00

    You could get teh current executable path by using "GetModuleFileName()".. then append the path to your json file.

    -SM

    0 comments No comments

  4. John Le Roux 96 Reputation points
    2021-10-16T07:09:25.397+00:00

    Can you give an example of that? @SM I don't know how to use GetModuleFileName(), I have tried std::cout << GetModuleFileName(); to see the current path so I can find where the relative path to the file is but it didn't work.

    0 comments No comments