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/Desktop/noteLibrary.json',而不是'/ home/pi/noteLibrary.json'。請檢查一下。 – Evgeny
哇。我必須複製/粘貼最後8次嘗試之一的錯誤輸出才能完成此項工作。對不起,浪費你的時間。 – daGriggs