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. Reading a file with Qt
QtWS25 Last Chance

Reading a file with Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
35 Posts 10 Posters 6.3k 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.
  • E Offline
    E Offline
    EL-jos
    wrote on 27 Jul 2018, 15:43 last edited by
    #1

    Hello everyone

    as the title indicates, I'm looking to read an entire file with Qt.

    when I read a small size file it is not a problem but when I try to read for example a large size file that there is a problem, my problem is that the package(QByteArray) is not displayed in the console(so it is always empty), here is my code:

    #include <QApplication>
    #include <QFileDialog>
    #include <QFile>
    #include <QDebug>
     
    int main(int argc,char** argv)
    {
        QApplication app(argc,argv);
     
        // Path to the target file to copy
        QString chemin = QFileDialog::getOpenFileName(nullptr,"Path to the file");
     
        // We have our file that we pass as a parameter the path to our target file
        qDebug() << chemin;
        QFile fichier(chemin);
     
        fichier.open(QIODevice::ReadOnly);
        if(fichier.isOpen()){
     
            qDebug() << "File opened and here is the name: " << fichier.fileName();
     
            QFileInfo info(fichier);
            QString extension = info.suffix();
     
            QByteArray paquet;
     
            while(!fichier.atEnd()){
     
                paquet = fichier.readAll();// full reading of our byte file
            }
     
            if(!paquet.isEmpty()){
     
               qDebug() << "The data has been well serialized in the byte packet.";
               qDebug() << paquet;
            }
        }
        return app.exec();
    }
    

    Note: I will read an.txt file of 3.0MB knowing that it is a file that contains genre words from a dictionary.

    in fact if I Serialize and deserialize my dictionary it is because I have to send it into a network,

    so the serialization will be during sending to the network and the de-serialization will be at the destination.

    Thanks in advance for your help.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Jul 2018, 15:50 last edited by mrjj
      #2

      Hi
      Tried you code directly
      with
      https://norvig.com/big.txt
      which is 6MB file
      and it just read it in.

      "E:/big.txt"
      File opened and here is the name: "E:/big.txt"
      The data has been well serialized in the byte packet. 6488666

      so are u sure your text file is just 3 MB ?

      1 Reply Last reply
      4
      • E Offline
        E Offline
        EL-jos
        wrote on 27 Jul 2018, 16:13 last edited by EL-jos
        #3

        @mrjj said in Reading a file with Qt:

        Hi
        Tried you code directly
        with
        https://norvig.com/big.txt
        which is 6MB file
        and it just read it in.
        "E:/big.txt"
        File opened and here is the name: "E:/big.txt"
        The data has been well serialized in the byte packet. 6488666
        so are u sure your text file is just 3 MB ?

        Of course, yes, just 3MB.

        then your result has an error because the package must display all the contents of the file in the console but at home your result is :
        The data has been well serialized in the byte packet. 6488666

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SamurayH
          wrote on 27 Jul 2018, 18:14 last edited by
          #4

          Hi @EL-jos,
          Be sure that your paquet contains the file's content, the problem could be with qDebug or the QtCreator's console.If you want to check that out you can use qlabel or qtextBrowser (it will consume some cpu for larger files), or QByteArray::mid.

          QLabel *label = new QLabel(paquet);
          label->showMaximized();
          //--------------------------
          qDebug() << paquet.mid(0,100);
          

          I don't know if it's a length limitation by qDebug, but I don't think so since it isn't mentioned in the documentation.
          You're not the first one, check out that thread to, I took the second line of code from it.

          "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mchinand
            wrote on 27 Jul 2018, 18:26 last edited by
            #5

            What is the size of paquet after you have read from your file? Does it match the size of the file in bytes? qDebug() has a limited buffer that is probably the issue in this case. Is there other processing of this data that you do after reading it in? Does that still work? Also, if you are reading in a text file, you might want to use QTextStream instead of QByteArray.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 27 Jul 2018, 18:30 last edited by
              #6

              @EL-jos said in Reading a file with Qt:

                  while(!fichier.atEnd()){
              
                      paquet = fichier.readAll();// full reading of our byte file
                  }
              

              When here readAll() does not return the complete content with the first call, you will not read all... you want paquet += ...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              5
              • E Offline
                E Offline
                EL-jos
                wrote on 27 Jul 2018, 19:48 last edited by
                #7

                Thanks for your help because I just noticed that my file was serialized in the QByteArray except the console could not display it in its entirety because of its buffer.

                But there is a new problem: when I try to send my QByteArray that contains my serialize file, when the QByteArray arrives at the server it loses all its data so its bytes.

                Please how to fix this problem?
                Knowing that I use the TCP protocol.

                J 1 Reply Last reply 27 Jul 2018, 19:56
                0
                • E EL-jos
                  27 Jul 2018, 19:48

                  Thanks for your help because I just noticed that my file was serialized in the QByteArray except the console could not display it in its entirety because of its buffer.

                  But there is a new problem: when I try to send my QByteArray that contains my serialize file, when the QByteArray arrives at the server it loses all its data so its bytes.

                  Please how to fix this problem?
                  Knowing that I use the TCP protocol.

                  J Online
                  J Online
                  JonB
                  wrote on 27 Jul 2018, 19:56 last edited by
                  #8

                  @EL-jos
                  From the information you've supplied on the new problem, I would guess some code is wrong somewhere.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    EL-jos
                    wrote on 27 Jul 2018, 20:21 last edited by
                    #9

                    Here's my code on the client side:

                    void Controleur::on_pushButton_clicked()
                    {
                        QString chemin = QFileDialog::getOpenFileName(this,"Chemin du fichier");
                        cheminFichier->setText(chemin);
                    
                        QFile fichier(chemin);
                        QFileInfo info(fichier);
                        QString extension = info.suffix();
                    
                        if(fichier.exists()){
                    
                           qDebug() <<"LE fichier existe bel est bienet et voici son extension: " << extension;
                    
                           int reponse = QMessageBox::question(this,"Confirmation d'envie du fichier", "êtes-vous sûr d'<strong>envoyer</strong> le fichier: " + fichier.fileName(), QMessageBox::Yes | QMessageBox::No);
                           if(reponse == QMessageBox::Yes){
                    
                               qDebug("Donc vous souhaitez envoyer le fichier dans le réseau.");
                    
                               fichier.open(QIODevice::ReadOnly);
                               if(fichier.isOpen()){
                    
                                   qDebug() << "Fichier ouvert et voici le nom: " << fichier.fileName();
                    
                                   QByteArray paquetFichier;
                    
                                   while(!fichier.atEnd()){
                    
                                       paquetFichier = fichier.readAll();// lecture entier de notre fichier en octet
                                   }
                    
                                   fichier.close();
                    
                                   if(!paquetFichier.isEmpty()){
                    
                                      qDebug() << "Les données ont été bien sérialiser dans le paquet en octets.";
                                      qDebug() << paquetFichier;
                                   }
                    
                    
                                   QByteArray paquetEnvoieFichier;
                                   QDataStream outFichier(&paquetEnvoieFichier,QIODevice::WriteOnly);
                    
                                   outFichier << (quint16) 0;
                                   outFichier << (quint16) 6;
                                   outFichier << extension;
                                   outFichier << paquetFichier;
                                   outFichier.device()->seek(0);
                                   outFichier << (quint16) (paquetEnvoieFichier.size() - sizeof(quint16));
                                   m_client->getSocket()->write(paquetEnvoieFichier);
                               }// Fin fichier isOpen
                              } // Fin MessageBox::YES
                           } // Fin si le fichier existe
                    }
                    
                    

                    and this is the one for reading

                    if(id == (quint16) 6){
                    
                            qDebug() << "réception d'un fichier venant du serveur.";
                    
                            QString extension;
                            QByteArray  paquetReception;
                    
                            in >> extension;
                            in >> paquetReception;
                    
                            qDebug() << "voici l'extension du fichier: " << extension <<" le paquet est: " <<paquetReception;
                    
                            QFile fichier("AutreFichier."+ extension);
                            fichier.open(QIODevice::WriteOnly);
                    
                            if(fichier.isOpen()){
                    
                                qDebug("Le fichier de la copie se -t- ouvert en écriture seul.");
                                qDebug() << "avec l'extension: " + extension;
                    
                                QDataStream outAutreFichier(&fichier);
                                outAutreFichier << paquetReception;
                            }
                    
                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 28 Jul 2018, 04:37 last edited by
                      #10

                      My comment is still not fixed... and now - what do you think will happen when your file size is greater than 65535 bytes?

                      outFichier << (quint16) (paquetEnvoieFichier.size() - sizeof(quint16));
                      And then you don't write the data directly to a file but stream it as QDataStream - this will add some more information in the front which is definitly not what you want... use
                      QFile::write(QByteArray())

                      And when sending data via network, readyRead() will for sure not contain the whole content - at least not when you're sending 6MB. Please take a look at the QTcpServer/Client examples (and avoid using QDataStream here - it's not needed at all...)

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      2
                      • E Offline
                        E Offline
                        EL-jos
                        wrote on 28 Jul 2018, 09:55 last edited by
                        #11

                        Yes I have this problem because I noticed that if my file size is larger than 65535 bytes, the server does not receive all the information. But how do we solve this problem?

                        Then if I use Qdatastream this to first write the identifier of my message because the one that says if it is a text message (5), if it is a new connection (3), if it is a disconnect (4) and for the file the identifier value is (7) to tell the server that it is a file. Here the QDATASTREAM allows me to write a screen like this:

                        *Message size

                        *Identifier

                        *Message

                        So if I don't use a Qdatastream how can I write in my QByteArray?

                        Translated with www.DeepL.com/Translator

                        aha_1980A 1 Reply Last reply 28 Jul 2018, 10:31
                        0
                        • E EL-jos
                          28 Jul 2018, 09:55

                          Yes I have this problem because I noticed that if my file size is larger than 65535 bytes, the server does not receive all the information. But how do we solve this problem?

                          Then if I use Qdatastream this to first write the identifier of my message because the one that says if it is a text message (5), if it is a new connection (3), if it is a disconnect (4) and for the file the identifier value is (7) to tell the server that it is a file. Here the QDATASTREAM allows me to write a screen like this:

                          *Message size

                          *Identifier

                          *Message

                          So if I don't use a Qdatastream how can I write in my QByteArray?

                          Translated with www.DeepL.com/Translator

                          aha_1980A Offline
                          aha_1980A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on 28 Jul 2018, 10:31 last edited by
                          #12

                          Hi @EL-jos,

                          What do you want to do exactly?

                          For file transfer, decicated protocols exists, e.g. FTP or HTTP. To me it looks like your are re-inventing the wheel?

                          Regards

                          Qt has to stay free or it will die.

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            EL-jos
                            wrote on 28 Jul 2018, 10:47 last edited by EL-jos
                            #13

                            Concretely I want my client to send a file to my server and the server after receiving the file, that it sends this file to all the client that are connected to it.
                            Because it is a chat software then I also want the client to be able to send a file to another client via a server.

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 28 Jul 2018, 10:51 last edited by
                              #14

                              Please take a look at the examples (again...) how to send large data ... http://doc.qt.io/qt-5/qtnetwork-loopback-example.html

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              2
                              • E Offline
                                E Offline
                                EL-jos
                                wrote on 28 Jul 2018, 11:02 last edited by
                                #15

                                Okay Thanks, I'll read this example and then I'll come back to you to give you the follow-up.

                                Thank you

                                1 Reply Last reply
                                0
                                • E Offline
                                  E Offline
                                  EL-jos
                                  wrote on 28 Jul 2018, 17:31 last edited by
                                  #16

                                  Hi,
                                  after several studies of the example given by the Qt documentation, I don't know where and how to send my file to the server?

                                  then in the example given by the Qt documentation, just sends the same 64KB packet several times until it tints 50MB . and the server and the client are in the same project but in my case the client and the server are different projects

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 28 Jul 2018, 19:39 last edited by
                                    #17

                                    Hi,

                                    Are you writing both the client and server parts ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • E Offline
                                      E Offline
                                      EL-jos
                                      wrote on 28 Jul 2018, 19:48 last edited by
                                      #18

                                      Yes, do you want me to mail them?

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 28 Jul 2018, 19:50 last edited by
                                        #19

                                        It was regarding your last remark about the server and client being in the same project. Since you are writing both, then it doesn't matter that they are in different projects.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        1
                                        • E Offline
                                          E Offline
                                          EL-jos
                                          wrote on 2 Aug 2018, 20:50 last edited by
                                          #20

                                          Hey, everybody,

                                          Still in the example given by the Qt documentation for the exchange of large documents between the client and the server in a chatt software, I could separate the client and the server in different projects which is not the case in the example given by the Qt documentation.

                                          So here's my question:

                                          Just that there I do not know how to adapt this example to my chatt software to be able to send large files between client and server?

                                          Translated with www.DeepL.com/Translator

                                          1 Reply Last reply
                                          0

                                          5/35

                                          27 Jul 2018, 18:26

                                          topic:navigator.unread, 30
                                          • Login

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