[Qt-interest] Char format in a text file

Andreas Pakulat apaku at gmx.de
Fri Apr 16 11:35:34 CEST 2010


On 16.04.10 09:04:58, Gian Marco Gallo wrote:
> The problem is this: I want to read a txt file and to wrtie a utf-8 xml.
> Obviously if the txt file is in ascii or in another format the xml reports
> strange chars. So my idea was to try to understand via c++ and qt the format
> of the file and then write a xml file in the same format.

As Thiago said, if you don't know the encoding of the file before reading
it, this is impossible in the general case. There are various algorithms
applying heuristics that try to find out the encoding of a file based on
its content, but they don't work in all cases. Browsers usually have
something like that to cope with broken servers sending no encoding
information for websites.

So your options are letting the user select the encoding of the file,
applying such an algorithm and hope you'll never hit a false-positive or a
combination of the two.

Also if the input is plain ascii you won't have any problems when reading
the file content into a QString as the default encoding used for reading a
file will be latin1 which covers ascii.

Andreas

-- 
You will triumph over your enemy.



More information about the Qt-interest-old mailing list