J'ai finalement trouvé un fichier de chemin QFile accepterait QFile.exist() et une bonne dose d'essais et d'erreurs.QIODevice :: read: périphérique non ouvert
Je veux savoir pourquoi les travaux suivants:
#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"
sortie réussie:
"/home/pi/noteLibrary.json"
"280C4"
Mais ce qui suit ne fonctionne pas:
#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"
Sortie d'erreur:
NO FILE HERE
QIODevice::read (QFile, "/home/pi/Desktop/noteLibrary.json"): device not open
""
Pourquoi QFile les traite-t-il différemment? Est-ce un problème de format QString? Ou est-ce le fait que je le déploie à distance sur un Raspberry Pi 3?
Le deuxième morceau de code ne peut pas faire cette sortie car la variable 'path' n'est pas déclarée ici. Le vrai chemin que vous donnez à 'QFile' est'/home/pi/Desktop/noteLibrary.json' et non '/ home/pi/noteLibrary.json'. Vérifie ça, s'il te plaît. – Evgeny
Wow. Je dois avoir copié/collé la mauvaise sortie d'un des 8 derniers essais pour que cela fonctionne. Désolé de perdre votre temps à ce sujet. – daGriggs