[solved] QTextstream problems
-
in header, QFile *file_contenente_elencoParole; QTextStream *leggi_scrivi;
@
Gestore::Gestore(QObject *parent) :
QObject(parent)
{
QDir dir;
dir.mkpath("C:/WoerterErinnerung");
file_contenente_elencoParole = new QFile("C:/WoerterErinnerung/elencoParoleSalvate.txt");if(file_contenente_elencoParole->open(QFile::Text | QFile::ReadWrite | QFile::Append)) { leggi_scrivi = new QTextStream(file_contenente_elencoParole); leggi_scrivi->setCodec("UTF-8"); } leggi_scrivi.operator <<("NEW LINE THAT LEGGI_SCRIVI CAN NOT WRITE....");}
@why leggi_scrivi do not write into the file?
-
Why do you use pointers here?
I would prefer using local stack variables:@
Gestore::Gestore(QObject *parent) :
QObject(parent)
{
QDir dir;
dir.mkpath("C:/WoerterErinnerung");
QFile file_contenente_elencoParol("C:/WoerterErinnerung/elencoParoleSalvate.txt");if(file_contenente_elencoParole.open(QFile::Text | QFile::ReadWrite | QFile::Append)) { QTextStream leggi_scrivi(&file_contenente_elencoParole); leggi_scrivi.setCodec("UTF-8"); leggi_scrivi << "NEW LINE THAT LEGGI_SCRIVI CAN NOT WRITE...."; }}
@ -
Global? uähhh....
perhaps as member in a class, but I would not use globaö variiables if possible.But you could call "QTextStream::flush":http://doc.qt.nokia.com/4.7/qtextstream.html#flush
-
If you are using [[Doc:QTextStream]] then please do not set the QFile::Text flag, that's useful for plain files only.
Also, you should use QIODevice::xxx flags, as those are defined in [[Doc:QIODevice]] not in [[Doc:QFile]].
This works:
@
QFile file("/tmp/qdn-test.txt");
if(file.open(QIODevice::WriteOnly | QIODevice::Append)) {
QTextStream ts(&file);
ts.setCodec("UTF-8");
ts << "Test for DevNet Users" << "\n";
ts << "more text" << "\n";
file.close();
}
@The file contents are likely to be not written to the disk until the file is closed, so you might see some leaks.
-
ok, also why this snippet dows not work?
@
QFile file_contenente_elencoParole("C:/WoerterErinnerung/elencoParoleSalvate.txt");
if(file_contenente_elencoParole.open(QIODevice::ReadWrite | QIODevice::Append))
{
QTextStream leggi_scrivi(&file_contenente_elencoParole);
leggi_scrivi.setCodec("UTF-8");
leggi_scrivi << "prova" << endl;
qDebug() << leggi_scrivi.readLine();
}
@