Reading a file with Qt
-
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 useQTextStream
instead ofQByteArray
. -
@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 += ...
-
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. -
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. -
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; }
-
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...)
-
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
-
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
-
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. -
Please take a look at the examples (again...) how to send large data ... http://doc.qt.io/qt-5/qtnetwork-loopback-example.html
-
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
-
Hi,
Are you writing both the client and server parts ?
-
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.
-
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
-
There's nothing special to do, if you implement the client and the server the same way as the exemple, then it's also going to work the same.
-
@EL-jos
Hi
As mr @Christian-Ehrlicher noted higher up.
You are using quint16 for size handling
Try
#include <limits.h>
qDebug() << std::numeric_limits<quint16>::max();and you will see the magic 65535 as maximum value.