codec of QTextStream

If you use QTextStream to read and write files, sooner or later, you will encounter the codec problem. Interestingly, Qt uses utf-8 string almost anywhere but QTextStream does not use utf-8 codec by default. In fact QTextStream uses a codec called locale codec to read/write files. You can use the static function QTextCodec::codecForLocale() to get the locale codec on your system.

UseĀ  QTextCodec::codecForLocale()->name().constData() to get the name of the locale codec. It is the same as

textstream.codec() ->name().constData() by default.

Knowing this, you can understand why it would be problematic to store utf-8 string to a file. To correctly save utf-8 strings to a file, you should set the utf-8 codec for the textstream first:

textstream.setCodec(“UTF-8”);

After doing this, the output file will be utf-8 encoded(without a BOM).

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Comments are closed, but trackbacks and pingbacks are open.