QTextStream - No data in File
-
wrote on 29 Oct 2020, 08:11 last edited by VRonin
Hi All
I am having problems with writing data to a text file using QTextStream.
The file is created but has nothing in it.
I am missing something so obvious.
Thanks in advance.
Code snippets below.
int cTask::processNewTransactions(QString accName, QString tmpFileName, QString transFileName, vector<cCategory> &catList) { int retVal = OK; QFile tFile(transFileName); QFile oFile(tmpFileName); QString line; string token; int tokenId = 0; bool bHeaderRow = true; cTransaction objTrans; std::string::size_type sz = 0; // alias of size_t if(!tFile.open(QIODevice::ReadOnly | QIODevice::Text)) { cout << "ERROR: Unable to open Transactions File\n"; emit finished(); } if(!oFile.open(QIODevice::WriteOnly | QIODevice::Text)) { cout << "ERROR: Unable to open Work File\n"; emit finished(); } QTextStream outFile(&oFile); // Write header to output file outFile << QString("Account,") << QString("Date,") << QString("ID,") << QString("Spare,") << QString("Type,") << QString("Text,") << QString("Amount,") << QString("Balance,") << QString("Group,") << QString("Sub Group,") << QString("Classification") << endl; transModel->clearData(); while (!tFile.atEnd()) { line = tFile.readLine(); if(!bHeaderRow || bNoHeaderRow) { std::istringstream iss(line.toStdString()); tokenId = 0; objTrans.txAccName = accName; ——— Snip Code to built objTrans ——— Snip updateCategory(objTrans, catList); //cout << objTrans; // << endl; outFile << objTrans << endl; transModel->addRow(objTrans); } bHeaderRow = false; } outFile.flush(); oFile.close(); tFile.close(); return retVal; }
-
Hi All
I am having problems with writing data to a text file using QTextStream.
The file is created but has nothing in it.
I am missing something so obvious.
Thanks in advance.
Code snippets below.
int cTask::processNewTransactions(QString accName, QString tmpFileName, QString transFileName, vector<cCategory> &catList) { int retVal = OK; QFile tFile(transFileName); QFile oFile(tmpFileName); QString line; string token; int tokenId = 0; bool bHeaderRow = true; cTransaction objTrans; std::string::size_type sz = 0; // alias of size_t if(!tFile.open(QIODevice::ReadOnly | QIODevice::Text)) { cout << "ERROR: Unable to open Transactions File\n"; emit finished(); } if(!oFile.open(QIODevice::WriteOnly | QIODevice::Text)) { cout << "ERROR: Unable to open Work File\n"; emit finished(); } QTextStream outFile(&oFile); // Write header to output file outFile << QString("Account,") << QString("Date,") << QString("ID,") << QString("Spare,") << QString("Type,") << QString("Text,") << QString("Amount,") << QString("Balance,") << QString("Group,") << QString("Sub Group,") << QString("Classification") << endl; transModel->clearData(); while (!tFile.atEnd()) { line = tFile.readLine(); if(!bHeaderRow || bNoHeaderRow) { std::istringstream iss(line.toStdString()); tokenId = 0; objTrans.txAccName = accName; ——— Snip Code to built objTrans ——— Snip updateCategory(objTrans, catList); //cout << objTrans; // << endl; outFile << objTrans << endl; transModel->addRow(objTrans); } bHeaderRow = false; } outFile.flush(); oFile.close(); tFile.close(); return retVal; }
wrote on 29 Oct 2020, 08:16 last edited by@Settz said in QTextStream - No data in File:
The file is created but has nothing in it.
At a glance code looks OK (you obviously got it from some very old post here?). You won't see anything in it necessarily till after
outFile.flush(); oFile.close();
, are you looking in the file before then? -
Hi All
I am having problems with writing data to a text file using QTextStream.
The file is created but has nothing in it.
I am missing something so obvious.
Thanks in advance.
Code snippets below.
int cTask::processNewTransactions(QString accName, QString tmpFileName, QString transFileName, vector<cCategory> &catList) { int retVal = OK; QFile tFile(transFileName); QFile oFile(tmpFileName); QString line; string token; int tokenId = 0; bool bHeaderRow = true; cTransaction objTrans; std::string::size_type sz = 0; // alias of size_t if(!tFile.open(QIODevice::ReadOnly | QIODevice::Text)) { cout << "ERROR: Unable to open Transactions File\n"; emit finished(); } if(!oFile.open(QIODevice::WriteOnly | QIODevice::Text)) { cout << "ERROR: Unable to open Work File\n"; emit finished(); } QTextStream outFile(&oFile); // Write header to output file outFile << QString("Account,") << QString("Date,") << QString("ID,") << QString("Spare,") << QString("Type,") << QString("Text,") << QString("Amount,") << QString("Balance,") << QString("Group,") << QString("Sub Group,") << QString("Classification") << endl; transModel->clearData(); while (!tFile.atEnd()) { line = tFile.readLine(); if(!bHeaderRow || bNoHeaderRow) { std::istringstream iss(line.toStdString()); tokenId = 0; objTrans.txAccName = accName; ——— Snip Code to built objTrans ——— Snip updateCategory(objTrans, catList); //cout << objTrans; // << endl; outFile << objTrans << endl; transModel->addRow(objTrans); } bHeaderRow = false; } outFile.flush(); oFile.close(); tFile.close(); return retVal; }
@Settz You should also add QIODevice::Append to open() call if you do not want to lose old file content each time you call processNewTransactions().
-
wrote on 29 Oct 2020, 23:53 last edited by
-
So simply open the file, write something into it and see if there is content in it without all the other loops directly at the start of your function.
-
So simply open the file, write something into it and see if there is content in it without all the other loops directly at the start of your function.
wrote on 30 Oct 2020, 09:02 last edited by@Christian-Ehrlicher Thankyou - good idea - did that but nothing
-
Then I would guess you overwrite it somewhere else.
-
wrote on 30 Oct 2020, 21:39 last edited by
Thankyou all for your help - it is working now.
@Christian-Ehrlicher You put me on the right track.
In another part of the application I had a bug that would reopen the file for writing !
3/8