2

I have a file where I want to write/ append unicode data to, because it is already unicode data (created by WMIC file output). It starts with UTF-16 (LE) BOM: 0xFF 0xFE.

I'd like to append some information in a loop:

  QString line = QString("%1,%2,%3,%4\n")
    .arg( node )
    .arg( *it )
    .arg( sDisplayName )
    .arg( sDisplayVersion );
  out.write( line.toLatin1().data() );

Where:

QFile out;
out.setFileName(filename);

I have tried different things. I thought QStrings themselves where Unicode. But I think I am missing something -- like setting the Encoding. It seems my appended characters are written as ASCII (only taking one byte each).

Thanks for your help!

Cheers Matthias

Edit: ok maybe the problem is also Latin1 <-> UTF-16?

matthias
  • 247
  • 6
  • 17
  • What encoding does your file use? If it also uses UTF-16, why would you convert your string to Latin-1? Just append the string as is, without the BOM, and minding the file's endinanness of course. `QString` considers its content to be UTF-16-encoded already. – Kerrek SB Jun 22 '11 at 10:45
  • Which file do you mean? The existing one I want to write in? It has got an UTF-16 BOM. I just don't know how to handle differen encodings. How can I write the already UTF-16-encoded QString into that file? I already have tried to go via QByteArray. Maybe I should use datastream? Or is it okay this way? – matthias Jun 22 '11 at 11:58
  • @Matt: You only mention one file in your question, `out`. But your question is not perfectly clear, avoid pronouns like "it" if that's ambiguous. Which data is where in what format? If your file is in UTF-16 and your string is in UTF-16, just append the string to the file, doesn't that work? – Kerrek SB Jun 22 '11 at 12:00
  • 1
    @Matt: How about using [QTextStream](http://doc.qt.nokia.com/latest/qtextstream.html#details) to wrap your `QFile` and write the string directly? – Kerrek SB Jun 22 '11 at 12:05
  • Ok, there is only one file (`out`). There already is data in the file in Unicode, which is UTF-16 (created by the WMIC output file redirection/ pipe). So I want to append some additional information to `out` with the same encoding, which is Unicode/ UTF-16. I am not sure what the QString in my program is -- I think it should be the same encoding. But using the write function for QFiles did not work out. Using `QTextStream out(&file);` and `out.setCodec("UTF-16");` and `out << myQString` finally does the thing I want! (I renamed the QFile from out to file.. just for confusion ;)) Thank you! – matthias Jun 22 '11 at 12:19
  • @Matt: The `QString` should be UTF-16 internally, but the raw `QFile::write()` takes a `char*` for writing, so you'd have to somehow expose an underlying uint16-buffer of the string and cast it to a char-buffer (of twice the length), which could be a hassle. The high-level TextStream seems like a much better approach. – Kerrek SB Jun 22 '11 at 12:29

1 Answers1

0

In the main add these two lines, which will make QString to use UTF-8:

QTextCodec::setCodecForTr( QTextCodec::codecForName( "UTF-8" ) );
QTextCodec::setCodecForCStrings( QTextCodec::codecForName( "UTF-8" ) );

And for appending, I found this link that may help.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260