Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. About QFile and QDatastream...
Forum Updated to NodeBB v4.3 + New Features

About QFile and QDatastream...

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    streetsoul
    wrote on last edited by VRonin
    #1

    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??

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • S streetsoul

      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??

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @streetsoul If you want to write text you should use http://doc.qt.io/qt-5/qtextstream.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • S streetsoul

        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??

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        5
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved