【发布时间】:2015-02-13 16:42:32
【问题描述】:
我有一个包含以下内容的配置文件:
{
"ip": "127.0.0.1",
"heartbeat": "1",
"ssl": "False",
"log_severity": "debug",
"port":"9999"
}
我使用 JsonCpp 来读取上述配置文件的内容。读取配置文件的内容工作正常,但写入配置文件的内容失败。我有以下代码:
#include <json/json.h>
#include <json/writer.h>
#include <iostream>
#include <fstream>
int main()
{
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
Json::StyledStreamWriter writer;
std::ifstream test("C://SomeFolder//lpa.config");
bool parsingSuccessful = reader.parse( test, root );
if ( !parsingSuccessful )
{
// report to the user the failure and their locations in the document.
std::cout << "Failed to parse configuration: "<< reader.getFormattedErrorMessages();
}
std::cout << root["heartbeat"] << std::endl;
std::cout << root << std::endl;
root["heartbeat"] = "60";
std::ofstream test1("C://SomeFolder//lpa.config");
writer.write(test1,root);
std::cout << root << std::endl;
return 0;
}
代码在控制台中打印正确的输出,但是当代码执行时配置文件是空的。如何使这段代码工作?
【问题讨论】:
-
如果你明确地关闭()测试输入流会发生什么?
-
关闭文件有效。我真傻:P。你能写出答案让我接受它让其他人知道正确答案吗?
-
完成,我已将我的提示评论“移植”到答案中。