Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我试图打开一个json文件。并将其存储为json对象。_C++_Json - Fatal编程技术网

C++ 我试图打开一个json文件。并将其存储为json对象。

C++ 我试图打开一个json文件。并将其存储为json对象。,c++,json,C++,Json,我试图打开一个具有json扩展名的文件,并将其存储为对象。但是,会不断收到一条错误消息,说明未在作用域中声明文件名。我不熟悉使用json文件。您对待它们是否与普通文本文件不同 #include "json.hpp" #include <iostream> #include <stdio.h> #include <fstream> #include <string> int main(int argc, char** argv) { std

我试图打开一个具有json扩展名的文件,并将其存储为对象。但是,会不断收到一条错误消息,说明未在作用域中声明文件名。我不熟悉使用json文件。您对待它们是否与普通文本文件不同

#include "json.hpp"
#include <iostream> 
#include <stdio.h> 
#include <fstream>
#include <string>


int main(int argc, char** argv) {

std::ifstream file;
file.open(test.json);
nlohmann::json jsonObject;
// Store the contents filename into jsonObject
if (file.is_open()) {
  file >> jsonObject;

}
file.close();

}
#包括“json.hpp”
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
std::ifstream文件;
open(test.json);
nlohmann::json jsonObject;
//将内容文件名存储到jsonObject中
if(file.is_open()){
文件>>jsonObject;
}
file.close();
}

您通过了
test.json
作为
open
-函数的文件名。因此,编译器假定名为
test
的对象具有数据成员
json
。除非您在代码中定义了这样一个对象,否则编译器将告诉您范围中没有名为
test
的对象。这就是为什么

你可能是说

if (file.open("test.json")) {
   ...

file.open(“test.json”)
“…文件名未在作用域中声明。”与文件为json(或其他任何内容)无关。顺便提一下请务必逐字逐句地发布任何编译器错误。同时请将代码精简到最少的示例,因为它不符合主题。