Using text file in resources for read and write in to text file.
-
I wrote a code to take the text in QLineEdit and write it to the text file. But when giving the file path of the text file, I use the file path of my computer, for example C:\Users\pc\Desktop\blabla/blabla.txt. But when I run this program on another computer, it will not find the file path. I tried adding a text file to the Resources section, but I could not write in the file. How can I do that. I'm very new to QT and c++ and an amateur.
void SecondWindow::on_pushButton_8_clicked() { QFile file(":/new/text/text/test.txt"); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) return; { QTextStream out(&file); out << ui->newPassBox->text(); } }
-
Resource files are read-only (by design and not even possible to write during runtime when you think about it)
-
Resource files are read-only (by design and not even possible to write during runtime when you think about it)
@Christian-Ehrlicher How can I write in to text while program is running. Do you now any way to do this?
-
Write it to a real file. See also QStandardPaths.
-
Write it to a real file. See also QStandardPaths.
@Christian-Ehrlicher I can print to file. What I need is for the application to find the file path when I run it on another computer. How can I do that?
-
@Christian-Ehrlicher I can print to file. What I need is for the application to find the file path when I run it on another computer. How can I do that?
@TheCeylann said in Using text file in resources for read and write in to text file.:
How can I do that?
Someone has to copy the file from computer A to computer B. Or store the file somewhere in the internet where you can access it from every computer with internet connection. Don't know your usecase though.
-
@TheCeylann said in Using text file in resources for read and write in to text file.:
How can I do that?
Someone has to copy the file from computer A to computer B. Or store the file somewhere in the internet where you can access it from every computer with internet connection. Don't know your usecase though.
@Christian-Ehrlicher I will store the password inside the text file. Uploading it somewhere can be a problem. Is there a way to define the file inside the file folder instead of defining it as full path. We can call files that are in the same folder like HTML only by their names. Is there a way to do this in Qt c++?
-
@Christian-Ehrlicher I will store the password inside the text file. Uploading it somewhere can be a problem. Is there a way to define the file inside the file folder instead of defining it as full path. We can call files that are in the same folder like HTML only by their names. Is there a way to do this in Qt c++?
@TheCeylann You can use
QCoreApplication::applicationDirPath()
, likeQCoreApplication::applicationDirPath() + "/test.txt"
-
@TheCeylann You can use
QCoreApplication::applicationDirPath()
, likeQCoreApplication::applicationDirPath() + "/test.txt"
@Bonnie This is the completely wrong directory - see my answer above: https://forum.qt.io/topic/134385/using-text-file-in-resources-for-read-and-write-in-to-text-file/6
-
@Christian-Ehrlicher I will store the password inside the text file. Uploading it somewhere can be a problem. Is there a way to define the file inside the file folder instead of defining it as full path. We can call files that are in the same folder like HTML only by their names. Is there a way to do this in Qt c++?
@TheCeylann
I don't know what your last post means. But yes you can specify a plain filename without a path toQFile()
. The problem is where do you think that is sought relative to? It will be the current working directory your application has when it's running. And you essentially have no control over that, and don't know what it might be. It will not necessarily have any relation at all to where your executable is installed or where your other files involved in the build/installation might be.That is why you should use something based off
QStandardPaths
, so you know where it actually is. -
@Bonnie This is the completely wrong directory - see my answer above: https://forum.qt.io/topic/134385/using-text-file-in-resources-for-read-and-write-in-to-text-file/6
@Christian-Ehrlicher
The OP ask for a HTML-like way, so I feel he just want to store the file with the exe.
I'm sure he should also learn to use QStandardPaths. :) -
@Christian-Ehrlicher
The OP ask for a HTML-like way, so I feel he just want to store the file with the exe.
I'm sure he should also learn to use QStandardPaths. :) -
@Bonnie
But OP is asking to be able to write to file too, right? "HTML-like way" is not for writing to files, is it? -
@JonB Right, but there're still quite many portable applications choose to store their configuration file, which could be both read and written, with the exe file.
QStandardPaths is not very friendly to portable applications though.@Bonnie said in Using text file in resources for read and write in to text file.:
which could be both read and written, with the exe file.
Really? Under Linux??
And nowadays under Windows apps are installed into
Program Files
and Win 10+ doesn't allow users to write there any more, does it? -
@Bonnie said in Using text file in resources for read and write in to text file.:
which could be both read and written, with the exe file.
Really? Under Linux??
And nowadays under Windows apps are installed into
Program Files
and Win 10+ doesn't allow users to write there any more, does it? -
@JonB No, of course Windows!
Portable applications are those without installers, they wouldn't be installed into Program Files :)@Bonnie
I didn't know it was Windows-only, thought it might be X-platform. But I had added to my post that nowadays you cannot write into WindowsProgram Files
, can you?
Oh OK, you & I are cross-posting now.
Sometimes I run "portable apps" off a read-only USB stick....QStandardPaths is not very friendly to portable applications though.
What about e.g.
QStandardPaths::AppDataLocation
or otherApp...
ones? -
@Bonnie
I didn't know it was Windows-only, thought it might be X-platform. But I had added to my post that nowadays you cannot write into WindowsProgram Files
, can you?
Oh OK, you & I are cross-posting now.
Sometimes I run "portable apps" off a read-only USB stick....QStandardPaths is not very friendly to portable applications though.
What about e.g.
QStandardPaths::AppDataLocation
or otherApp...
ones?@JonB No, a typical usage of a portable application is coping its folder to an USB disk and you can put it on different computers and run it with remembering the configurations you already made.
How could theApp...
ones do that?
Sure there will be unwritable risks, but if the portablity is more important, then I have to accept that. -
@JonB No, a typical usage of a portable application is coping its folder to an USB disk and you can put it on different computers and run it with remembering the configurations you already made.
How could theApp...
ones do that?
Sure there will be unwritable risks, but if the portablity is more important, then I have to accept that.@Bonnie said in Using text file in resources for read and write in to text file.:
How could the App... ones do that?
Perhaps you had understood that the OP wishes to write to a file and then take that updated file to another computer, but I had not. Clearly that changes the equation! :)