0
QFile이 QFile.exist()를 사용하여 받아 들일 수있는 경로 파일과 많은 시행 착오를 발견했습니다.QIODevice :: read : 장치가 열려 있지 않음
내가 알고 싶은 이유를 다음 작품 :
이#include <QFile>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>
QString path = QDir::currentPath(); // Get current dir
path.append("/noteLibrary.json");
QFile file(path); // Give QFile current dir + path to file
if (!file.exists()) { // Check to see if QFile found the file at given file_path
qDebug() << "NO FILE HERE";
}
qDebug() << path; // See what path was finally successful
file.open(QIODevice::ReadOnly); // Continue parsing document to confirm everything else is functioning normally.
QByteArray rawData = file.readAll();
// Parse document
QJsonDocument doc(QJsonDocument::fromJson(rawData));
// Get JSON object
QJsonObject json = doc.object();
// Access properties
qDebug() << json["die"].toString(); // Should output "280C4"
성공적인 출력 :
"/home/pi/noteLibrary.json"
"280C4"
그러나 다음은 작동하지 않습니다
#include <QFile>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>
QFile file("/home/pi/noteLibrary.json"); // Give QFile current dir + path to file
if (!file.exists()) { // Check to see if QFile found the file at given file_path
qDebug() << "NO FILE HERE";
}
//qDebug() << path; // See what path was finally successful
file.open(QIODevice::ReadOnly); // Continue parsing document to confirm everything else is functioning normally.
QByteArray rawData = file.readAll();
// Parse document
QJsonDocument doc(QJsonDocument::fromJson(rawData));
// Get JSON object
QJsonObject json = doc.object();
// Access properties
qDebug() << json["die"].toString(); // Should output "280C4"
오류 출력 :
NO FILE HERE
QIODevice::read (QFile, "/home/pi/Desktop/noteLibrary.json"): device not open
""
왜 QFile에서 다르게 처리합니까? QString 형식의 문제입니까? 아니면 내가 이것을 원격으로 Raspberry Pi 3에 배포하여 사실을 비난 할 수 있습니까?
두 번째 코드는 path 변수가 선언되지 않았기 때문에이 출력을 수행 할 수 없습니다. 또한'QFile'에주는 실제 경로는'/ home/pi/noteLibrary.json'가 아니라'/ home/pi/Desktop/noteLibrary.json'입니다. 이것 좀 봐, 제발. – Evgeny
와우. 이 작업을하기 위해 지난 8 번의 시도 중 하나에서 잘못된 결과물을 복사/붙여 넣어야합니다. 이것에 시간을 낭비해서 죄송합니다. – daGriggs