Actually I am trying to parse a json file using rapidjson library . But when i am trying to add this header file in my code it shows me an error like this
"[Error] rapidjson/document.h: No such file or directory" and
"recipe for target 'main_1.o' failed"
here main_1 is my file name.
This is my actual code
#include<stdio.h>
#include "rapidjson/document.h"
using namespace rapidjson;
Document document;
document.Parse(json);
int main()
{
char name[50];
int t_value;
return 0;
}
And also i haven't idea about where i want to add my json file?
But i really don't know where i did a mistake? please anyone help me.
Kindly check the below link for installation of json, you can also visit rapidjson official website
RapidJson installation
And for your code:
Download all header files of rapidjson and keep it inside your current folder under rapidjson folder(new folder)
Write the below code inside main, compiler error will occur due to this.
Document document;
document.Parse(json);
If you are using Ubuntu then package manager can be used to install the rapidjson lib
$ sudo apt-get update
$ sudo apt-get install rapidjson-dev
The path of the rapidjson include for me was
/usr/include/rapidjson
and in the cpp/hpp file
#include <rapidjson/document.h>
worked for me
sample program to load file
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/ostreamwrapper.h>
int main()
{
using namespace rapidjson;
std::ifstream ifs { R"(myfile.json)" };
if ( !ifs.is_open() )
{
std::cerr << "Could not open file for reading!\n";
return EXIT_FAILURE;
}
IStreamWrapper isw { ifs };
Document doc {};
doc.ParseStream( isw );
StringBuffer buffer {};
Writer<StringBuffer> writer { buffer };
doc.Accept( writer );
if ( doc.HasParseError() )
{
std::cout << "Error : " << doc.GetParseError() << '\n'
<< "Offset : " << doc.GetErrorOffset() << '\n';
return EXIT_FAILURE;
}
const std::string jsonStr { buffer.GetString() };
std::cout << jsonStr << '\n';
std::cout <<"done\n";
return EXIT_SUCCESS;
}
Demo code Source:
How to read json file using rapidjson and output to std::string?
Related
i have made a example for try the JsonCpp library.
I have included it in my project, the project content is this:
#include <cstdlib>
#include <string>
#include <fstream>
#include <iostream>
#include <json\value.h>
#include <json\json.h>
using namespace std;
int main()
{
Json::Reader reader; //for reading the data
Json::Value newValue; //for modifying and storing new values
Json::StyledStreamWriter writer; //for writing in json files
//opening file using fstream
ifstream file("items.json");
// check if there is any error is getting data from the json file
if (!reader.parse(file, newValue)) {
cout << reader.getFormattedErrorMessages();
exit(1);
}
cout << newValue["Category"] << endl;
file.close();
system("pause");
}
The json file name is items.json and its content is this:
{
"Category" : "Technical",
"Date" : "1 January 2021",
"Name" : "Java2Blog",
"first" : "Shishank",
"last" : "Jain"
}
But when i compile and run the project, it generate this error:
* Line 1, Column 1
Syntax error: value, object or array expected.
I have followed this guide: https://java2blog.com/json-parser-cpp/
this is my first time using json in a C ++ project
I have solved my problem.
The json file was already encoded in UTF-8, and the file path was correct.
I have changed my code like this:
#include <fstream>
#include <iostream>
#include <json\json.h>
using namespace std;
int main()
{
ifstream file;
file.open("items.json");
if (!file)
{
cout << "File non esiste" << endl;
}
else
{
Json::Reader reader; //for reading the data
Json::Value value; //for modifying and storing new values
reader.parse(file, value);
cout << value["Category"] << endl;
}
file.close();
system("pause");
}
I apologize to everyone for the inconvenience
I'm trying to figure out how to write to a file outside the working directory. This is the code I currently have.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string sp{};
std::fstream ss("C:\\Users\\onion\\AppData\\Roaming\\MetaQuotes\\Terminal\\some numbers\\MQL5\\Files\\testnew.txt", std::ios::in | std::ios::out);
if (!ss.is_open()) std::cout << "Failed" << '\n';
else
{
while (ss.is_open())
{
std::getline(ss, sp);
std::cout << sp << '\n';
ss << "new data";
if (ss.eof())break;
}
}
}
I can read the file perfectly fine, but I cant write to it? Could it be that Metatrader itself is limiting my ability to write to a file or does a file have to be in the working directory to be able to write to it? or am I just doing it wrong?
I am trying read a text file which has Valid JSON content but not string. The below code works fine if it is a string dump. For example - if file contents are like this "{ \"happy\": true, \"pi\": 3.141 }" then it will parse without errors. Now I want to find out a way which minimizes these conversion ? How to convert JSON content to String dump in C++ using any standard lib? I am using nlohmann for now, but seems like this requires additional coding. Please educate me if I can hack this with simple code.
My Code
#include <iostream>
#include <fstream>
#include <streambuf>
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;
int main()
{
std::fstream f_json("C://json.txt");
json jFile;
try {
jFile = json::parse(f_json);
}
catch (json::parse_error &e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
Our client produces JSON files which is like below.
{
"happy": true,
"pi": 3.141
}
My file is under C:/test.json, so it dint had the permission to open it. Now I placed it in proper folder. Now its working fine.
I like to use ThorsSerializer. Disclaimer I wrote it.
#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include <sstream>
#include <iostream>
#include <string>
struct MyObj
{
bool happy;
double pi;
};
ThorsAnvil_MakeTrait(MyObj, happy, pi);
Example Usage:
int main()
{
using ThorsAnvil::Serialize::jsonImport;
using ThorsAnvil::Serialize::jsonExport;
std::stringstream file(R"({ "happy": true, "pi": 3.141 })");
MyObj data;
file >> jsonImport(data);
std::cout << jsonExport(data) << "\n";
}
Output:
{
"happy": true,
"pi": 3.141
}
It works the same for a file stream. But you should not escape the " characters in the file.
I am new to C++.
I was trying to read a file using fstream.
here is the code,
I put the file inside the a.out directory but still cannot read it, where is my mistake?
#include<iostream>
#include<fstream>
int main()
{
std::ifstream myfile("my.txt");
int a, b;
while(myfile>>a>>b)
std::cout<<a<<b;
return 0;
}
Try:
#include <iostream>
#include <fstream>
#include <unistd.h>
int main()
{
char* name = get_current_dir_name();
std::cout << "Current Working Dir: " << name << "\n";
free(name);
std::ifstream myfile("my.txt");
if (!myfile))
{
std::cout << "Failed to open file\n";
exit(1);
}
int a, b;
while(myfile>>a>>b)
{
std::cout<<a<<b;
}
return 0;
}
Make sure that the file is located in the current directory of the .exe. This is usually the same directory as where the .exe is located on your harddrive.
If you don't know what the current directory is, I recommended you use the full path.
I want to use liblas to treat some data so I installed the library on my ubuntu 13.04 at /usr/share/include , the automatic path.
I have also a project on code::blocks to use this library.
here is an extract of the main program :
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <liblas.hpp>
using namespace std;
int main()
{
std::ifstream ifs;
ifs.open("myfile.LAS", std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
liblas::Header const& header = reader.GetHeader();
std::cout << "Compressed: " << (header.Compressed() == true) ? "true":"false";
std::cout << "Signature: " << header.GetFileSignature() << '\n';
std::cout << "Points count: " << header.GetPointRecordsCount() << '\n';
return 0;
}
When I want to compile it, I got the error "liblas/version.hpp : no such file or directory"
Code::Blocks opens liblas.hpp and shows the error :
$#include liblas/version.hpp$
(under <>)
but in fact, there is in the same folder as liblas.hpp a file called version.hpp
What's wrong?