Solved QFile can't find the file
-
I'm trying to read data from a .txt file but my QFile can't read the data from it.
I've tried using "F:/Database/visi.txt" as the address and it works fine. But when I try to use "visi.txt" and put the file in the Debug folder and also the project folder, it can't read it anywhere. I've also tried using QCoreApplication::applicationDirPath() + "visi.txt" but it still can't seem to read it.
I'm new to Qt and don't know how to fix it. Maybe it is because that my Project, Debug and Release folder is on the Desktop. I want my .txt to be in the same folder as the project/application and still be able to read it if I change the directory folder of the application with the txt file. -
Hi and welcome the forums.
I assume you need it outside the app (editable), so using qresource is a no go.QCoreApplication::applicationDirPath() is the right way but i think you need
QString fullpath = QCoreApplication::applicationDirPath() + "/visi.txt"
if inside the database folder that is NEXT to the exe. it should be
QString fullpath = QCoreApplication::applicationDirPath() + "/DataBase/visi.txt" -
Hi and welcome to devnet forum
This one QCoreApplication::applicationDirPath() + "visi.txt" should be probably QCoreApplication::applicationDirPath() + "/visi.txt" otherwise you are changing only the last folder name and extend it.
If you go on the left pane to "projects" and check under "Build&Run" -> "Run" you can see the working directory when you start your application through Qt creator
-
@koahnig @mrjj
I tried to do it using
QCoreApplication::applicationDirPath() + "/visi.txt"
but my program was still unable to open it.QString fEmp = QCoreApplication::applicationDirPath() + "/visi.txt"; QFile ReadFrom(fEmp); if(!ReadFrom.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::information(this, "File Error", "This file does not exist or there was an error while reading it."); return; }
This is the code I'm using to open the file. Everytime I press the button where this code is connected to, it gives me the message written in QMessageBox. I put the visi.txt in all the folders: Debug, Release and the project folder but the error was still there.
-
@aesthetically-asthmatic
put it next to where .exe is.
also do
qDebug() << fEmp;
and check the path.