[Interest] QFile::size() and QTextStream::pos()

Igor Mironchik igor.mironchik at gmail.com
Thu Oct 29 03:51:36 CET 2015



On 29.10.2015 00:43, Thiago Macieira wrote:
> On Thursday 29 October 2015 00:36:08 Igor Mironchik wrote:
>> This is good, but what if I need to seek stream to the one character
>> back position? Can I simple write
>>
>> stream.seek( stream.pos() - 1 );
>>
>> or I have to keep in mind codec? If so how can I seek to one character
>> back position and don't worry about codecs, etc...
> Avoid doing that. Seeking the stream is an expensive operation since
> QTextStream needs to flush the buffers, reposition the device and reconvert
> the input into UTF-16 without Carriage Return.
>
> I don't know what happens if you position it after the CR but before the LF. I
> know what happens if you position in the middle of a multibyte sequence: the
> first character you'll read will be either mojibake or the Unicode Replacement
> Character (U+FFFD).

It's sadly. But you are completely right. Even more: QTextStream::pos() 
much more expensive then QTextStream::seek(). In my first implementation 
I did some reading/parsing ... then calculating of percent of 
completeness of parsing using QTextStream::pos() and emiting signal with 
calculated percent. Very simple. But parsing of ~100 KB file with such 
calculations using QTextStream::pos() takes ~25 seconds. When simple 
commenting using QTextStream::pos() reduce parsing time to 1-2 seconds.

And I had to use QFile::pos() for such a case. But it wasn't so simple 
as I described, because in some cases I needed to seek stream to some 
position... And I refused even from seek operations...

So now parsing of such file is just a moment... :)

P.S. Why QTextStream doesn't have something like get(), peek(), unget(), 
putback()?

-- 
Best Regards,
Igor Mironchik.




More information about the Interest mailing list