rapidjson:用于从文件中读取文档的工作代码
rapidjson: working code for reading document from file?
我需要一个可用的c++代码来使用rapidjson从文件中读取文档:https://code.google.com/p/rapidjson/
在wiki中,它还没有文档记录,示例仅从std::string中进行了非序列化,我对模板没有深入的了解。
我将文档序列化为一个文本文件,这是我编写的代码,但它没有编译:
#include "rapidjson/prettywriter.h" // for stringify JSON
#include "rapidjson/writer.h" // for stringify JSON
#include "rapidjson/filestream.h" // wrapper of C stream for prettywriter as output
[...]
std::ifstream myfile ("c:\statdata.txt");
rapidjson::Document document;
document.ParseStream<0>(myfile);
编译错误状态:错误:"Document"不是"rapidjson"的成员
我将Qt 4.8.1与mingw和rapidjson v0.1一起使用(我已经尝试过升级v0.11,但错误仍然存在)
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <fstream>
using namespace rapidjson;
using namespace std;
ifstream ifs("test.json");
IStreamWrapper isw(ifs);
Document d;
d.ParseStream(isw);
请阅读中的文档http://rapidjson.org/md_doc_stream.html。
@Raanan的答案中的FileStream
显然不受欢迎。源代码中有一条注释,说要使用FileReadStream
。
#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
using namespace rapidjson;
// ...
FILE* pFile = fopen(fileName.c_str(), "rb");
char buffer[65536];
FileReadStream is(pFile, buffer, sizeof(buffer));
Document document;
document.ParseStream<0, UTF8<>, FileReadStream>(is);
刚刚在遇到类似问题后发现了这个问题。解决方案是使用FILE*对象,而不是ifstream和rapidjson自己的FileStream对象(您已经包含了正确的头)
FILE * pFile = fopen ("test.json" , "r");
rapidjson::FileStream is(pFile);
rapidjson::Document document;
document.ParseStream<0>(is);
你当然需要添加文档.h-include(这回答了你的直接问题,但不会解决你的问题,因为你使用了错误的文件流):
#include "rapidjson/document.h"
然后,文档对象(我可能会添加得相当快)被文件内容填充。希望它能有所帮助!
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在文档文件夹中创建目录
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 关于类的 Python 文档 - 对C++的引用不正确
- C++/autoconf 等效于文档库要求.txt的要求是什么?
- mbed:使用 USB 文档库编译会导致错误
- C++ VISUAL STUDIO:GLFWwindow没有初始化,即使它是在文档中编写的并且以前工作过
- 使用 yaml-cpp 更新 YAML 文档的节点和值
- 在加载 MSHTML 文档之前从 MSHTML 文档中删除无效的 URL
- 如何在C++中表示JSON文档的递归对象结构?
- 如何查找 Redshift Maya API 文档?
- 如何使用值模板编写文档测试用例?
- 如何使用 Gradle DSL(领域特定语言)上的文档?
- ncurses 找不到任何关于cchar_t的文档
- 从Windows上的系统工作的服务中获取用户文档文件夹.C++
- 我怎样才能使野牛文档的 calc++ 示例工作
- 如何将数据文件加载到自动Excel工作表文档
- v8javascript API文档在c中工作
- rapidjson:用于从文件中读取文档的工作代码