Code:
out << "TESTING"; file.close();
after i run above code the text file bcome
Attachment 951
pls help me slove this problem...
thx
Printable View
Code:
out << "TESTING"; file.close();
after i run above code the text file bcome
Attachment 951
pls help me slove this problem...
thx
Use QTextStream instead of QDataStream. The latter is for serialization.
i m going to use QTcpSocket to send a QByteArray(which carry a file's content) to receiver.
i use QDataStream here, bcoz the file can be any type(text file, picture file and etc).
wat i did in my sender program is:
is it any solution available?Quote:
quint64 maxSize;
QByteArray Data;
QFile file(fileName);
Data = file.read(maxSize);
QTcpSocket *tcpSocket = new QTcpSocket;
tcpSocket->abort();
tcpSocket->connectToHost(targetIP,targetPort);
QByteArray block;
QDataStream out_s(&block,QIODevice::WriteOnly);
out_s.setVersion(QDataStream::Qt_4_0);
out_s << (quint64)0;
out_s << Data;
out_s.device()->seek(0);
out_s << (quint64)(block.size() - sizeof(quint64));
tcpSocket->write(block);
tcpSocket->disconnect();
i should use quint64 or quint16 here?
1. Seeking in a socket device doesn't make sense as it is not a random access device.
2. If you use QDataStream, you don't need to send the size of QByteArray as QDataStream will do that for you. Remember that this is a serialization mechanism, not a simple binary stream.
3. connectToHost() is an asynchronous call - you have to wait for the connection to be established before sending/receiving any data. The same goes for disconnectFromHost (I think you meant that and not disconnect(), right?).
thx wysota.
QTextStream slove my 1st problem in writing text to a file.
i apply it in writing a picture file with QDataStream.Quote:
QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream textstream(&file);
textstream << text;
but nothing appear in the picture file.
wat should i do?
You can't use a text stream to handle binary data... I suggest you don't use streams at all with binaries or use QDataStream::readRawData and QDataStream::writeRawData instead of << and >> operators.