2017-01-10 272 views
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可能是責任?

+0

第二段代碼無法執行此輸出,因爲'path'變量沒有在那裏聲明。你給'QFile'的真正路徑是'/ home/pi/Desktop/noteLibrary.json',而不是'/ home/pi/noteLibrary.json'。請檢查一下。 – Evgeny

+0

哇。我必須複製/粘貼最後8次嘗試之一的錯誤輸出才能完成此項工作。對不起,浪費你的時間。 – daGriggs

回答

0

無論我上面的代碼出了什麼問題,使用下面的代碼給QFile,絕對路徑確實同樣可以用currentPath()創建一個QString。我一定有其他錯誤,我的錯誤!

noteLibrary.json

{"note": [{ 
      "profile": "C4", 
      "die": "280C4", 
      "pressure": 800, 
      "position": 10000 
     }, 
     { 
      "profile": "CC4", 
      "die": "2280C4", 
      "pressure": 8800, 
      "position": 110000 
     } 

    ], 
    "test": { 
     "profile": "CCC4", 
     "die": "22280C4", 
     "pressure": 88800, 
     "position": 1110000 
    } 
} 

main.cpp中摘錄

QFile file("/home/pi/noteLibrary.json"); 
    if (!file.exists()) qDebug() << "NO FILE FOUND"; 
    file.open(QIODevice::ReadOnly); 
    QByteArray rawData = file.readAll(); 
    QJsonDocument doc(QJsonDocument::fromJson(rawData)); // Parse document 
    QJsonObject jObj = doc.object(); // Get JSON object 
    qDebug() << jObj["test"]; 

應用程序輸出

QJsonValue(object,QJsonObject({"die":"22280C4","position":1110000,"pressure":88800,"profile":"CCC4"})) 

似乎奇怪的是它顯示的屬性值按字母順序排列,不文件中列出的順序。