About QFile and QDatastream...
Unsolved
General and Desktop
-
I try save data to text file using QFile and QDatastream.
For this function, I add the source code like below.void MainWindow::on_saveButton_clicked() { QString fileName = QFileDialog::getSaveFileName(this, tr("Export File"), "", tr("Text (*.txt)::All Files (*)")); if(fileName.isEmpty()) return; else { QFile file(fileName); if(!file.open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("Unable to open file"), file.errorString()); return; } QDataStream out(&file); out.setVersion(QDataStream::Qt_5_10); out << "test"; } }
I hope to just exist "text" message in text file.
But the result is below...
"NUL NUL NUL ENO test NUL"What is the NUL, ENO?? And how can I remove this message??
-
@streetsoul If you want to write text you should use http://doc.qt.io/qt-5/qtextstream.html
-
@streetsoul
Hi, what you see there before the text is probably what QDataStream made out of the QString header, so that you can read it from the file back into a QString without problems.Anyway, if you want to write a Human-readable textfile, than QDataStream is the wrong choice.
Use QTextStream, and open the file with
QIODevice::Text
flag.