how to open a txt file (using QFile) getting current file from Qdir?
-
wrote on 2 Jan 2019, 23:45 last edited by
Hi,
i'm using qt creator (macos) and i need to open a txt file file using QFile from the current directory using Q Dir.code:
QFile file(QDir::current().absolutePath() + "/peatefinal.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;This code already get the path but it don't open the file. I tried "currentPath ()" as well but it didn't work too. Think that the problem could be related to the separator because the current path uses "/" and a need to use "//" to open manually. How can i solve this problem?
Cheers
-
hi and welcome to the forums.
did you try
qDebug() << QDir::current().absolutePath();
to make sure its the path you expect ?
Also can you explain a bit more what you mean by having to use
"//" to open manually ? -
wrote on 3 Jan 2019, 00:28 last edited by
@mrjj said in how to open a txt file (using QFile) getting current file from Qdir?:
qDebug() << QDir::current().absolutePath();
Thank you very much for your answer. Now i guess that the problem is how can i put the txt file inside the directory. The file "peatefinal.txt" is saved on the same directory of the project but it didn't recognize the file when i run the project:
Code:
QFile file( "peatefinal.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;When i save the file to into the main directory and changed the code like this:
Code:
QFile file( "//peatefinal.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;It works perfectly.
So the problem now is: How can i save the txt file inside the same directory of the app if the file is already saved on the same directory of the project?
-
@mrjj said in how to open a txt file (using QFile) getting current file from Qdir?:
qDebug() << QDir::current().absolutePath();
Thank you very much for your answer. Now i guess that the problem is how can i put the txt file inside the directory. The file "peatefinal.txt" is saved on the same directory of the project but it didn't recognize the file when i run the project:
Code:
QFile file( "peatefinal.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;When i save the file to into the main directory and changed the code like this:
Code:
QFile file( "//peatefinal.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;It works perfectly.
So the problem now is: How can i save the txt file inside the same directory of the app if the file is already saved on the same directory of the project?
wrote on 3 Jan 2019, 01:20 last edited by Pl45m4 1 Mar 2019, 01:21@pedromenezes said in how to open a txt file (using QFile) getting current file from Qdir?:
How can i save the txt file inside the same directory of the app if the file is already saved on the same directory of the project?
So you want to save your txt file in the directory where your program is executed?
// This returns your appDir QCoreApplication::applicationDirPath()
Idk if this is what you are looking for.
The file is saved in your app (which app? Qt Creator or your program?) directory and you want to (move?) save it in your project directory?
-
wrote on 3 Jan 2019, 01:42 last edited by pedromenezes 1 Mar 2019, 01:47
@Pl45m4 said in how to open a txt file (using QFile) getting current file from Qdir?:
QCoreApplication::applicationDirPath()
Thank's for your help. The app I'm doing "DirOpen2" reads some .txt files and plots on a chart. It turns out, I do not know why, even these files being in the same folder as my app I can not read them. It looks like they are not being copied to the same folder as my application.
The code below did not find the txt file in the app appendix (this folder seems to be hidden i did not find it by the Finder):
QFile file(QCoreApplication::applicationDirPath() + "/peatefinal.txt"); ui->label->setText(QCoreApplication::applicationDirPath() + "/peatefinal.txt"); if(!file.exists()){ ui->textBrowser->setText("The file was not found"); } if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; ui->label_2->setText("The file was not opened");
The first label is: /Users/pedromenezes/Desktop/00_Arquivos/04_Profissional/05_LATEC/03_Desenvolvimento/x_Testes/build-OpenDir2-Desktop_Qt_5_12_0_clang_64bit-Debug/OpenDir2.app/Contents/MacOS
The textBox shows: "The file was not found"
But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.
However, if I save the file of interest ("peatefinal.txt") in the main directory and change the code to:
QFile file("//peatefinal.txt");
it works.
This file: "peatefinal.txt" is already saved in the same folder of the project. But if i change the code to:
QFile file("peatefinal.txt");
it doesn't work.
cheers
-
@Pl45m4 said in how to open a txt file (using QFile) getting current file from Qdir?:
QCoreApplication::applicationDirPath()
Thank's for your help. The app I'm doing "DirOpen2" reads some .txt files and plots on a chart. It turns out, I do not know why, even these files being in the same folder as my app I can not read them. It looks like they are not being copied to the same folder as my application.
The code below did not find the txt file in the app appendix (this folder seems to be hidden i did not find it by the Finder):
QFile file(QCoreApplication::applicationDirPath() + "/peatefinal.txt"); ui->label->setText(QCoreApplication::applicationDirPath() + "/peatefinal.txt"); if(!file.exists()){ ui->textBrowser->setText("The file was not found"); } if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; ui->label_2->setText("The file was not opened");
The first label is: /Users/pedromenezes/Desktop/00_Arquivos/04_Profissional/05_LATEC/03_Desenvolvimento/x_Testes/build-OpenDir2-Desktop_Qt_5_12_0_clang_64bit-Debug/OpenDir2.app/Contents/MacOS
The textBox shows: "The file was not found"
But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.
However, if I save the file of interest ("peatefinal.txt") in the main directory and change the code to:
QFile file("//peatefinal.txt");
it works.
This file: "peatefinal.txt" is already saved in the same folder of the project. But if i change the code to:
QFile file("peatefinal.txt");
it doesn't work.
cheers
wrote on 3 Jan 2019, 02:23 last edited by@pedromenezes said in how to open a txt file (using QFile) getting current file from Qdir?:
But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.
It's the name of your app in the buid folder.
To look at "OpenDir2.app/Contents/MacOS"
right click on your app and choose "show package contents" and navigate to MacOs folder.Anyway, using the current path on a desktop app is meaningless AMHA. On Mac the current path is not the same if you run your app from the desktop !
Use QStandardPaths class to read/save your files to standard locations.
-
@pedromenezes said in how to open a txt file (using QFile) getting current file from Qdir?:
But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.
It's the name of your app in the buid folder.
To look at "OpenDir2.app/Contents/MacOS"
right click on your app and choose "show package contents" and navigate to MacOs folder.Anyway, using the current path on a desktop app is meaningless AMHA. On Mac the current path is not the same if you run your app from the desktop !
Use QStandardPaths class to read/save your files to standard locations.
wrote on 3 Jan 2019, 02:31 last edited by@mpergand Thank you very much. Problem solved!
-
@mpergand Thank you very much. Problem solved!
@pedromenezes
maybe you want to look into QStandardPath -class, could come in handy for you as wellDon't forget to set the topic to solved when your question is answered ;-)
-
Hi,
On a side note: don't modify the application bundle content like that. It's bad practice and it will also likely fail depending on how your application will be deployed.
As @J-Hilk suggested, QStandardPaths to get the correct folder where to store your application data.
-
wrote on 5 Jan 2019, 05:15 last edited by pedromenezes 1 May 2019, 10:31
Thanks again! I solved the problem using:
QString path = qApp->applicationDirPath(); path.append("/peate.txt"); QFile fileUrl(path);
But I will study the QStandardPaths to use it.
1/10