View Full Version : problem in writing text with QFile
wei243
4th March 2007, 14:18
QFile file(fileName);
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out << "TESTING";
QApplication::restoreOverrideCursor();
file.close();
after i run above code the text file bcome
951
pls help me slove this problem...
thx
wysota
4th March 2007, 14:24
Use QTextStream instead of QDataStream. The latter is for serialization.
wei243
5th March 2007, 15:57
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:
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();
is it any solution available?
i should use quint64 or quint16 here?
wysota
5th March 2007, 16:25
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?).
wei243
6th March 2007, 14:06
thx wysota.
QTextStream slove my 1st problem in writing text to a file.
QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream textstream(&file);
textstream << text;
i apply it in writing a picture file with QDataStream.
but nothing appear in the picture file.
wat should i do?
wysota
6th March 2007, 14:26
i apply it in writing a picture file with QDataStream.
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.