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. Write data in TXT
Qt 6.11 is out! See what's new in the release blog

Write data in TXT

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.3k Views 1 Watching
  • 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.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    Hello everyone.
    First of all thanks for helping me and reading this post.

    I would like to write a std::map data in a .TXT.

    I write this code:

    void MainWindow::on_Save_TXT_button_clicked()
    {
        QString file_save = QFileDialog::getSaveFileName(this, "Guardar TXT", "/home", "files TXT (*.txt)");
    
            if(!file_save.isEmpty()) //If the user have choosen a file then:
            {
                QFile file_s(file_save); //The QFile class can read and write files. Here we do that namefile will be QFile type with name file.
    
    
                if(file_s.open(QFile::WriteOnly))
                {   
                fopen(file_save,"w");
                fprintf(....)
                fclose(...)
                //TODO SAVE THE DATA MAP
                }
            }
    }
    

    With that, the user can specify the file name and path.
    But the problem appears when I use fopen because file_save is not a FILE it is a QString.
    So how can I use that with fopen, fprinf and fclose?

    Thanks a lot.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Why try to mix QFile and old c, fopen fwrite etc?
      You can do it all with Qt classes

      #include <QString>
      #include <QFile>
      #include <QDebug>
      #include <QTextStream>
      
      int main(int argc, char **argv)
      {
          /* Try and open a file for output */
          QString outputFilename = "Results.txt";
          QFile outputFile(outputFilename);
          outputFile.open(QIODevice::WriteOnly);
      
          /* Check it opened OK */
          if(!outputFile.isOpen()){
              qDebug() << argv[0] << "- Error, unable to open" << outputFilename << "for output";
              return 1;
          }
      
          /* Point a QTextStream object at the file */
          QTextStream outStream(&outputFile);
      
          /* Write the line to the file */
          outStream << "Victory!\n";
      // u write ur members of the map.
      
          /* Close the file */
          outputFile.close();
          return 0;
      }
      
      AlvaroSA 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        Why try to mix QFile and old c, fopen fwrite etc?
        You can do it all with Qt classes

        #include <QString>
        #include <QFile>
        #include <QDebug>
        #include <QTextStream>
        
        int main(int argc, char **argv)
        {
            /* Try and open a file for output */
            QString outputFilename = "Results.txt";
            QFile outputFile(outputFilename);
            outputFile.open(QIODevice::WriteOnly);
        
            /* Check it opened OK */
            if(!outputFile.isOpen()){
                qDebug() << argv[0] << "- Error, unable to open" << outputFilename << "for output";
                return 1;
            }
        
            /* Point a QTextStream object at the file */
            QTextStream outStream(&outputFile);
        
            /* Write the line to the file */
            outStream << "Victory!\n";
        // u write ur members of the map.
        
            /* Close the file */
            outputFile.close();
            return 0;
        }
        
        AlvaroSA Offline
        AlvaroSA Offline
        AlvaroS
        wrote on last edited by
        #3

        @mrjj Thanks a lot my friend.
        But how can I acces to spaces and \n in QTextStream?
        Because I need a \n in each line.

        so like this:
        line1
        line2
        line3
        .
        .
        .

        maybe with void QTextStream::setFieldWidth(int width)?

        Thanks a lot again.

        mrjjM 1 Reply Last reply
        1
        • AlvaroSA AlvaroS

          @mrjj Thanks a lot my friend.
          But how can I acces to spaces and \n in QTextStream?
          Because I need a \n in each line.

          so like this:
          line1
          line2
          line3
          .
          .
          .

          maybe with void QTextStream::setFieldWidth(int width)?

          Thanks a lot again.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AlvaroS

          outStream << "line1" << "\n" << "line2" << "\n";

          and space is just " ".

          Or do I misunderstand what you ask?

          AlvaroSA 1 Reply Last reply
          0
          • mrjjM mrjj

            @AlvaroS

            outStream << "line1" << "\n" << "line2" << "\n";

            and space is just " ".

            Or do I misunderstand what you ask?

            AlvaroSA Offline
            AlvaroSA Offline
            AlvaroS
            wrote on last edited by
            #5

            @mrjj Thanks a lot my friend. It works really fine!!!!!!!!

            mrjjM 1 Reply Last reply
            1
            • AlvaroSA AlvaroS

              @mrjj Thanks a lot my friend. It works really fine!!!!!!!!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @AlvaroS
              Super :)
              The best benefit of using a QTextStream is that
              most of Qt classes have overload the << so they can just be saved.

              Note: Be careful with pointers
              QWidget *w;

              stream << w; will write the address
              stream << *w; will write the object (if object have << defined)

              AlvaroSA 1 Reply Last reply
              1
              • mrjjM mrjj

                @AlvaroS
                Super :)
                The best benefit of using a QTextStream is that
                most of Qt classes have overload the << so they can just be saved.

                Note: Be careful with pointers
                QWidget *w;

                stream << w; will write the address
                stream << *w; will write the object (if object have << defined)

                AlvaroSA Offline
                AlvaroSA Offline
                AlvaroS
                wrote on last edited by
                #7

                @mrjj Yes!!! I was careful with that and It worked really fine! Thanks again!

                1 Reply Last reply
                1

                • Login

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