PDA

View Full Version : Read/Write from QTextStream



tonde
28th July 2008, 14:03
I've just started with QT and I found something I can't understand.

Why won't this work?


QTextStream out("test",QIODevice::ReadWrite);
out<<"hello";
QString string;
out >> string;

Only "test" is added to the string. Couldn't find a way to append "hello" to the string.

jacek
28th July 2008, 22:05
The docs say:

QTextStream::QTextStream ( const QByteArray & array, QIODevice::OpenMode openMode = QIODevice::ReadOnly )
Constructs a QTextStream that operates on array, using openMode to define the open mode. The array is accessed as read-only, regardless of the values in openMode.

You have to use a different constructor to create an RW stream:
QString buffer( "test" );
QTextStream out( &buffer, QIODevice::ReadWrite );
out << "hello";
QString string;
out >> string; // == "testhello"

If you just want to append a string use QString::operator+() or QString::append().

tonde
29th July 2008, 07:08
The docs say:

You have to use a different constructor to create an RW stream:
QString buffer( "test" );
QTextStream out( &buffer, QIODevice::ReadWrite );
out << "hello";
QString string;
out >> string; // == "testhello"

OK, thanks! I tought that const string would use the "QString" constructor. Apparently it uses the "const ByteArray" constructor.



If you just want to append a string use QString::operator+() or QString::append().
My intention was to learn QTextStream... :)

tonde
29th July 2008, 07:50
There is more to come
It seems that if I try to assign QByteArray to the QTextStream QT is once again using the IODevice::ReadOnly constructor. How can I assign QByteArray to the QTextStream in ReadWrite mode? Threre should be one according to the documentation (http://doc.trolltech.com/4.3/qtextstream.html#QTextStream-5)


QString buffer = "test";
QByteArray array;
array.append(buffer);
QTextStream out(&array,QIODevice::ReadWrite);
out << "hello";
QString string;
out >> string; // == "test"


I even get similar problems when using IODevices:

QTemporaryFile file("test");
if (!file.open()) //will always be opened in QIODevice::ReadWrite mode
return;
QTextStream inOut(&file);
inOut << "hello";
QString s;
inOut >> s; // == ""

What am I missing here?

jacek
30th July 2008, 22:47
For the file you need:
QTextStream inOut(&file);
inOut << "hello";
inOut.flush(); // write to file
inOut.device()->seek( 0 ); // rewind the file
QString s;
inOut >> s; // == "hello"
Unfortunately I don't know what's wrong with QByteArray case.

tonde
31st July 2008, 10:02
Thanks. I missed that seek() function completely. Implementing seek() to the QByteArray case seems to be working.

example:


QString buffer = "funny world";

QByteArray array;
array.append(buffer);
QTextStream out(&array,QIODevice::ReadWrite);

//out.pos() == 0
out << "hello"; //"hello" overwrites "funny"

out.seek(0); // set position to 0

QString string;
QString string2;

// stream == "hello world"
// QTextStream reads into strings word by word delimited by space
out >> string; // == "hello"
out >> string2; // == "world"

SunnySan
31st July 2008, 16:34
can we use the seek function with a delimiter?

e.g. |word1|word2|....
with delimiter "|"

jacek
31st July 2008, 16:51
can we use the seek function with a delimiter?
No, you have to use QString::indexOf() or similar method.