question

JohnLeRoux-0739 avatar image
0 Votes"
JohnLeRoux-0739 asked JohnLeRoux-0739 answered

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

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++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JohnLeRoux-0739 avatar image
0 Votes"
JohnLeRoux-0739 answered

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!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MinxinYu-MSFT avatar image
1 Vote"
MinxinYu-MSFT answered

Hi, @JohnLeRoux-0739

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.










5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JohnLeRoux-0739 avatar image
0 Votes"
JohnLeRoux-0739 answered

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? @MinxinYu-MSFT

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SM-4238 avatar image
0 Votes"
SM-4238 answered

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

-SM

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JohnLeRoux-0739 avatar image
0 Votes"
JohnLeRoux-0739 answered

Can you give an example of that? @SM-4238 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.