在C++中打开 json 文件时遇到问题
Having trouble opening a json file in C++
我正在尝试打开一个我将在C++中使用的 json 文件。 我之前成功使用的代码无法打开文件。 我在Windows 10 Pro上使用Visual Studio 2017,其中包含适用于现代C++3.5.0版本的JSON。
我有一个非常简单的函数,它应该打开一个文件作为 json 对象的输入。 它似乎打开了文件,但在将其写入 json 对象时中止。最初要打开的文件在另一个目录中,但是我在测试时将其移动到与可执行文件相同的目录中...但这并没有帮助。
下面是失败的非常短的函数:
json baselineOpenAndRead(string fileName) //passed string used for filename
{
json baseJObject;
cout << "we have a baseJObject" << endl;
//ifstream inFileJSON("test_file.json"); // Making this explicit made no difference
ifstream inFileJSON;
inFileJSON.open("test_file.json", ifstream::in);
cout << "we have opened json inFileJSON" << endl; // get here
inFileJSON >> baseJObject;
cout << " Can direct inFileJSON into baseJObject" << endl; //never get here; the app aborts.
inFileJSON.close();
return baseJObject;
}
这似乎与nlohmann网站上的示例基本相同:
// read a JSON file
std::ifstream i("file.json");
json j;
i >> j;
我只是希望这会打开 json 文件,将其加载到对象中,然后返回对象。 相反,它只是退出。
感谢您的任何想法...即,我做错了什么? (我将忽略它以前有效...也许我错过了一些东西(。
--铝
根据要求,这里有一个最小的可重现示例,但它需要 nlohmann 的 json.hpp 才能编译:
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
string fileName;
json baselineOpenAndRead(string);
int main(int argC, char *argV[])
{
json baseJObject;
if (argC != 2) // check to make sure proper number of arguments are given.
{
cout << "nnFilename needed...";
exit(1); // number of arguments is wrong - exit program
}
else
{
fileName = argV[1];
baseJObject = baselineOpenAndRead(fileName); // opens and reads the Base Line JSON file
cout << "baseJObject returned" << endl;
}
return 0;
}
json baselineOpenAndRead(string fileName) //
{
cout << "File name: " << fileName << endl;
json baseJObject;
cout << "we have a baseJObject" << endl;
ifstream inFileJSON(fileName);
if (inFileJSON.is_open())
{
cout << "file open..." << endl;
if (nlohmann::json::accept(inFileJSON))
{
cout << "valid json" << endl;
try { inFileJSON >> baseJObject; }
catch (const std::exception &e) { std::cout << e.what() << 'n'; throw; }
}
else
{
cout << "not valid json" << endl;
}
}
else
{
cout << "file not really open" << endl;
}
inFileJSON >> baseJObject;
cout << " We can echo inFileJSON into baseJObject" << endl;
inFileJSON.close();
return baseJObject;
}
我用这个json文件测试了它:
{
"people": [{
"name": "Scott",
"website": "stackabuse.com",
"from": "Nebraska"
},
{
"name": "Larry",
"website": "google.com",
"from": "Michigan"
},
{
"name": "Tim",
"website": "apple.com",
"from": "Alabama"
}
]
}
当我运行它时,将上面的 json 作为 data.json 传递,我得到以下输出,然后它退出:
./Test_json data.json
File name: data.json
we have a baseJObject
file open...
valid json
[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal
没有尝试,它就会退出。 它永远不会超过FileJSON>> baseJObject;
另一个似乎有效的尝试,但为什么呢?
还行。 我使用相同的主文件尝试了这个(唯一的变化是在函数中(:
json baselineOpenAndRead(string fileName) //
{
json baseJObject;
string filePath = "../baselines/" + fileName;
cout << "filePath: " << filePath << endl;
ifstream inFileJSON(fileName);
//baseJObject = json::parse(inFileJSON);
inFileJSON >> baseJObject;
cout << baseJObject << std::endl;
return baseJObject;
}
这对我来说看起来基本相同。 我尝试在原始和这个中制作ifstream inFileJSON(fileName.c_str(((。 原来的继续失败,这个继续工作。 抱歉,这太长了,但我无法从评论中获得体面的格式...... 我应该尝试回答我自己的问题吗?
我想我已经有了这个。 我相信我最初的问题是由我的一个 json 测试文件中的错误","引起的。 随后,if (inFileJSON.is_open( 工作了,但if (nlohmann::json::accept(inFileJSON(失败并导致相同(或类似(错误。 我认为我需要可执行文件目录之外的文件路径的 c_str((,但它似乎没有区别。 我取出了accept((,这段代码似乎始终如一地工作:
json baselineOpenAndRead(string fileName) //
{
json baseJObject;
cout << "we have a baseJObject" << endl;
string filePath = "../baselines/" + fileName;
cout << "filePath: " << filePath << endl;
//ifstream inFileJSON(filePath.c_str());
ifstream inFileJSON(filePath);
if (inFileJSON.is_open())
{
cout << "File is open." << endl;
inFileJSON >> baseJObject;
cout << baseJObject << std::endl;
inFileJSON.close();
return baseJObject;
}
else
{
cout << "File not open." << endl;
exit(1);
}
}
感谢大家的帮助。 我很感激。
--铝
- 如何从文本文件中读取数值,直到遇到字符类型?
- 创建 Spdlog 异步文件记录器时遇到困难
- 我在使用C++文件时遇到了一些问题
- 我在 ndk 中构建文件时遇到错误 android.mk
- 我遇到使用 c++ 逐行读取输入文件的问题
- 从文件导入数据时遇到问题.我有一个没完没了的循环
- 在visual Studio中构建代码时,我遇到错误,.h文件丢失
- 在C++中打开 json 文件时遇到问题
- C++读取文件时遇到困难
- 我在从.txt文件中加起来时遇到了一些问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 在C++中使用 Visual Studio 读取文本文件时遇到问题
- 使用 libzip 打开 zip 文件时遇到问题
- 我在编写从文本文件中读取数据并按升序打印的代码时遇到问题
- 当所有内容都在一个文件中时,为什么会遇到链接器错误
- 当文件遇到特定字符时,如何停止读取文件
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- C++处理文件,在制作程序时遇到麻烦