开发者

Qt: how to write/ append unicode data to an existing file

开发者 https://www.devze.com 2023-03-14 09:45 出处:网络
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 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消