File not found
-
hello,
I have a program that reads data from a json file and displays that data onto a display screen. I'm using QFileSystemWatcher so if there is a change in the file, then with signal/slot it will update the display with the correct new data. When i first run the code it opens the json file, pulls the data and displays it just fine. However, when i make a change to the data within the file, the signal gets sent and runs the code that pulls the new data from the file, except the file says it can't be found. So simply, the file opens the first time around, but can't be located any time after that. The file name nor the path changes.
//readJSON.cpp QString readJSON::readingJson(const QString &name) { QFile file; file.setFileName("C:/Users/RnThs/Desktop/Projects/data.json"); if(!file.exists()){ qDebug()<<"File does not exist"; exit(1); } if(!file.open(QIODevice::ReadOnly)){ qDebug()<<"Failed to open"; exit(1); } QTextStream file_text(&file); QString json_string; json_string = file_text.readAll(); file.close(); QByteArray json_bytes = json_string.toLocal8Bit(); auto json_doc=QJsonDocument::fromJson(json_bytes); QJsonObject json_obj=json_doc.object(); QVariantMap json_map = json_obj.toVariantMap(); return json_map[name].toString(); }
-
@texasRanger said in File not found:
However, when i make a change to the data within the file
How do you do that? Some external tool? My guess is that tool is keeping the file open so you cannot access it again? Or even renaming it...? Are you Windows?
except the file says it can't be found.
So does it hit your
"File does not exist"
or"Failed to open"
, you don't say which? -
@texasRanger said in File not found:
i manually went out to the file, made a change, and then closed the file.
Can't you just say how? If it was in, say, Notepad, why not say, "I did it in Notepad, and exited"?
Anyway that was my thought.
From the code you show you seem to be closing the file properly, might be good to put in a
qDebug()
to make sure. Are you sure you don't do something else to the file, like delete or rename it? Get rid of yourQFileSystemWatcher
and see? Is it possible to have a race condition betweenQFileSystemWatcher
doing something and you try theexists()
just too early? Put in a delay to determine. -
@JonB Oh yeah sorry.. I wasn't reading the right. It was opening in a VS text editor. I tried running it with notepad and it worked. I guess VS has something where they keep the file open, so i couldn't edit it and then update the display.