It's strange, I add desired file into the resources via Add Existing Files..., the file is there. I run qmake ("Build->Run qmake") to make the file available.
The first issue: I can't write anything into the file from output terminal! But when I manually write into the file, the output terminal shows the change every time I run it. Second issue: it still says QIODevice::read: device not open !
Here's my code:
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <iostream>
void wFile(QString Filename)
{
QFile nFile(Filename);
QTextStream str(&nFile);
qDebug() << "what do you want to write in the desired file: ";
str.readLine();
if (!nFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "could not open the file";
return;
}
nFile.flush();
nFile.close();
}
void read (QString Filename){
QFile nFile(Filename);
if(!nFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file for reading";
return;
}
QTextStream in(&nFile);
QString nText = in.readAll();
qDebug() << nText;
nFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString nFilename =":/MyFiles/DocumentArminV.txt";
wFile(nFilename);
read(nFilename);
return a.exec();
}
And here's output terminal of the code:
The files saved in a qresource are read-only since they are part of the executable so you can not write or modify them.
docs:
Currently, Qt always stores the data directly in the executable, even on Windows, macOS, and iOS, where the operating system provides native support for resources. ...
Related
It's strange, I add desired file into the resources via Add Existing Files..., the file is there. I run qmake ("Build->Run qmake") to make the file available.
The first issue: I can't write anything into the file from output terminal! But when I manually write into the file, the output terminal shows the change every time I run it. Second issue: it still says QIODevice::read: device not open !
Here's my code:
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <iostream>
void wFile(QString Filename)
{
QFile nFile(Filename);
QTextStream str(&nFile);
qDebug() << "what do you want to write in the desired file: ";
str.readLine();
if (!nFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "could not open the file";
return;
}
nFile.flush();
nFile.close();
}
void read (QString Filename){
QFile nFile(Filename);
if(!nFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file for reading";
return;
}
QTextStream in(&nFile);
QString nText = in.readAll();
qDebug() << nText;
nFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString nFilename =":/MyFiles/DocumentArminV.txt";
wFile(nFilename);
read(nFilename);
return a.exec();
}
And here's output terminal of the code:
The files saved in a qresource are read-only since they are part of the executable so you can not write or modify them.
docs:
Currently, Qt always stores the data directly in the executable, even on Windows, macOS, and iOS, where the operating system provides native support for resources. ...
It's strange, I add desired file into the resources via Add Existing Files..., the file is there. I run qmake ("Build->Run qmake") to make the file available.
The first issue: I can't write anything into the file from output terminal! But when I manually write into the file, the output terminal shows the change every time I run it. Second issue: it still says QIODevice::read: device not open !
Here's my code:
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <iostream>
void wFile(QString Filename)
{
QFile nFile(Filename);
QTextStream str(&nFile);
qDebug() << "what do you want to write in the desired file: ";
str.readLine();
if (!nFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "could not open the file";
return;
}
nFile.flush();
nFile.close();
}
void read (QString Filename){
QFile nFile(Filename);
if(!nFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file for reading";
return;
}
QTextStream in(&nFile);
QString nText = in.readAll();
qDebug() << nText;
nFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString nFilename =":/MyFiles/DocumentArminV.txt";
wFile(nFilename);
read(nFilename);
return a.exec();
}
And here's output terminal of the code:
The files saved in a qresource are read-only since they are part of the executable so you can not write or modify them.
docs:
Currently, Qt always stores the data directly in the executable, even on Windows, macOS, and iOS, where the operating system provides native support for resources. ...
I am new to Qt and I want to be able to select a already existing file called default.ini which is in the same directory as the exe. I already have some code that allows me to do that, but the user has to manually select the file everytime.
QString file = QFileDialog::getOpenFileName(this, tr("Open File"), "/debug", tr("default (*.ini)"));
if (file != NULL) {
try {
controller_->ConfigurationFileSave(file.toStdString());
} catch (std::exception &e) {
Logger::Log(std::string("Failed to save configuration: ") + e.what(),
Logger::kError);
}
}
The program does everything I want it to do in terms of writing/ reading to the file, I just don't want the program to require any user input in opening the file. I understand that the reason I have user input is because I am using the QFileDialog class and I just want to know if there is another class that does it automatically. Thanks
Edit 1 As per Arun's suggestion I tried to use Qfile. The program now reads from the default.ini config file successfully but it won't save to the config file. Is there an easy way to write to the file?
QFile file("default.ini");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QString content = file.readAll();
file.close();
if (content != NULL) {
try {
controller_->ConfigurationFileSave(content.toStdString());
} catch (std::exception &e) {
Logger::Log(std::string("Failed to save configuration: ") + e.what(),
Logger::kError);
}
}
Edit 2 As per Arun's second suggestion:
QFile file("default.ini");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QString line = file.readAll();
file.close();
file.open(QIODevice::WriteOnly | QIODevice::Text);
try {
controller_->ConfigurationFileSave(line.toStdString());
} catch (std::exception &e) {
Logger::Log(std::string("Failed to save configuration: ") + e.what(),
Logger::kError);
}
file.close();
Your file chooser code was passing a filename to ConfigurationFileSave(). Here's code to get that file name without interaction:
QDir appDir(QCoreApplication::applicationDirPath());
QFileInfo file(appDir, "default.ini");
// file.filePath() or file.absoluteFilePath() here:
controller_->ConfigurationFileSave( ___HERE___ );
You can test this in QtCreator by creating a new console application and using this as your main.c:
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QDir>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir appDir(QCoreApplication::applicationDirPath());
QFileInfo file(appDir, "default.ini");
qDebug() << " " << file.filePath();
qDebug() << " " << file.absoluteFilePath();
return 0;
}
Output:
silver:qt_app_dir hchapman$ ls
Makefile main.cpp main.o qt_app_dir qt_app_dir.pro qt_app_dir.pro.user
silver:qt_app_dir hchapman$ ./qt_app_dir
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
silver:qt_app_dir hchapman$ cd ..
silver:Desktop hchapman$ ./qt_app_dir/qt_app_dir
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
silver:Desktop hchapman$
Here is a typical example of how you can do file I/O without using QFileDialog.
This example here uses QFile
QFile file("default.ini");
if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
return;
while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}
I wrote a simple code to open plain text file with Qt 5's QFile as seen below;
// main.cpp
#include <iostream>
using std::endl;
using std::cout;
#include <QCoreApplication>
#include <QFile>
#include <QIODevice>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile plainFile("plain.txt");
if(plainFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
cout << "File opened successfull" << endl;
plainFile.close();
}
else{
cout << "could not open file." << endl;
}
return a.exec();
}
The output when compiled and run is "could not open file".
What am I do wrong?
Probably because plain.txt does not exist in the current working directory or in the PATH. Make sure the file is in the working directory or pass the absolute path to QFile.
Also see what QFile::exists returns.
Wouter Huysentruit is right.
Remember, that, by default, when you running application from QtCreator working directory is, for example, C:\Projects\build-Test-Desktop_Qt_5_1_0_MinGW_32bit-Debug. But your .exe file lay in C:\Projects\build-Test-Desktop_Qt_5_1_0_MinGW_32bit-Debug\Debug.
When you run your .exe directly, then working directory will be that folder, where this file is layed now.
So, you can:
1.Pass absolute path.
2.Put file in current working directory (I suppose that it is best solution).
3.Just change relative path: QFile plainFile("debug/plain.txt");
I have text files in my resource file and I'd like to be able to provide a path for this file to std::ifstream. Neither :\file_name.txt nor ..\file_name.txt works.
Does anyone know how to fix it?
Qt resource files are not filesystem files. Those files are loaded in memory as static char arrays. You can see for yourself looking in your build directory for qrc_*.cpp files. You can get data from there if you want, or you might want to use QTextStream for reading those, using the QIODevice constructor with a QFile.
You don't specify what you want to do exactly, but this is a sample that reads what is inside the file:
#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QFile>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile file(":/test.txt");
QTextStream stream(&file);
if (!file.open(QIODevice::ReadOnly)) {
qFatal("Failed to open file.");
return -1;
}
QString text = stream.readAll();
if (text.isNull()) {
qDebug("Failed to read file.");
return -1;
}
qDebug("File content is: %s. Bye bye.", qPrintable(text));
return 0;
}