I am trying to read a line from a .txt file and then write that line back into another .txt. However, I can only open the files in ReadOnly mode. When I try to write back to the second resource .txt it won't open.
In my project I added a resource file and added the two file "Test.txt" and "Ergebnis.txt" to the resource file which now looks like this:
<RCC>
<qresource prefix="/stuff">
<file>Test.txt</file>
<file>Ergebnis.txt</file>
</qresource>
<qresource prefix="/things"/>
The actual reading/writing is done in separate functions, which are called like so:
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QString mLine1 = "Success";
mLine = Read(":/stuff/Test.txt");
Write(":/stuff/Ergebnis.txt", mLine1);
}
And which look like so:
QString Dialog::Read(QString filename){
QFile mNewFile(filename);
QString* mText = new QString;
bool check = true;
if(!mNewFile.open(QFile::ReadOnly | QFile::Text)){
return *mText = "";
}
QTextStream in(&mNewFile);
check = in.readLineInto(mText);
mNewFile.close();
return *mText;
}
void Dialog::Write(QString out, QString Text){
QFile mNewFile(out);
QString errMsg;
QFileDevice::FileError err = QFileDevice::NoError;
if(mNewFile.exists())
qDebug() << "Ergebnis.txt exists!";
if(!mNewFile.open(QFile::WriteOnly | QFile::Text )){
errMsg = mNewFile.errorString();
err = mNewFile.error();
qDebug() << "Error: " << errMsg <<"Number: " << err;
return;
}
QTextStream out_str(&mNewFile);
out_str << Text;
mNewFile.flush();
mNewFile.close();
}
Reading works just fine, so I'm confident the paths to the resources are correct. In the Write() function I also checked whether the file exists, which it does. I tried opening the file in ReadOnly, which works as well. The problem seems to be that I can't open the file in WriteOnly or ReadWrite mode. The errMsg outputs "Unknown error" to the command line. The error code is "5", whatever that means...
I also tried opening the file directly from my filesystem using:
Write("/home/js/Dokumente/Cpp/Programme/QT/TestStringSource/Ergebnis.txt", mLine1);
which worked just fine, funnily enough.
Any advice is greatly appreciated!