[Qt-interest] Qt4.5.1: QTextStream input with 16bit characters

Rainer Wiesenfarth Rainer.Wiesenfarth at inpho.de
Mon Aug 9 17:25:10 CEST 2010


I have a problem with Qt4.5.1 and text files containing 16bit characters. A
hex representation of the files content looks like this:

  FFFE 2200 3100 2200 2000 3700 0D00 0A00
  2200 3200 2200 2000 3800 0D00 0A00

The first character of the file is the UTF Byte Order Mark. If i try to read
this file using the code below, I get the two lines (indented for
readability):

  "1" 7
  "2" 8

Here is the code snippet:

  QFile file (filename);

  if (file.open (QIODevice::ReadOnly | QIODevice::Text))
  {
    QTextStream stream (&file);
    QString     line;

    while (! stream.atEnd ())
    {
      line = stream.readLine ();

      if (line.startsWith (QChar::ByteOrderMark))
      {
        // This line is not reached
        line = line.mid (1);
      }
    }
  }

I would have assumed that QTextStream strips the Byte Order Mark character
(as it picks the appropriate QTextCodec for the file), but it does not. Even
worse, removing the Byte Order Mark with QString::startsWith() fails also,
as the character got converted.

My question: Where is my mistake? Or is this a (probably already solved)
"feature" of Qt4.5.1? Any guesses how to solve this?

Best Regards / Mit freundlichen Grüßen
Rainer Wiesenfarth

-- 
INPHO GmbH * Smaragdweg 1 * 70174 Stuttgart * Germany
phone: +49 711 2288 10 * fax: +49 711 2288 111 * web: www.inpho.de
place of business: Stuttgart * managing director: Johannes Saile
commercial register: Stuttgart, HRB 9586
Leader in Photogrammetry and Digital Surface Modelling
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 6878 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100809/7da96a0c/attachment.bin 


More information about the Qt-interest-old mailing list