2017-01-10 11 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/noteLibrary.json'가 아니라'/ home/pi/Desktop/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"})) 

는 알파벳 순서 속성 값을 표시하지 않도록 홀수 같다문서에 열거 된 순서.