PDA

View Full Version : QIODevice and QTextCodec?



whitefurrows
25th November 2010, 15:44
Hi,

i read files like this,


string entire;
char buf[1024];
while (device->readLine(buf, sizeof(buf)) && !device->atEnd()) { //QIODevice = device
entire += buf;
//...
}
size_t length = entire.length();

for (size_t i = 0; i < length; ++i) {
char c = entire[i];
//...
s.append(c);
}

but my QSting ("s") looks dirty. The problem is the file can be coded in utf8, iso 8859-15 and a lot more.

How can i read the file with the right codec, if the user choose the codec before?

I have try QTextCodec::setCodecForCStrings( QTextCodec::codecForName( codec ) ) but i hink thats not right. Can you help me pleas?

whitefurrows
26th November 2010, 14:08
Hi,

to describe the problem exact, with QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8")) is the result:
Special character: ??,??,??,??,??,??,??,???

and without QTextCodec::setCodecForCStrings():
Special character: �,ä,�,ö,�,ü,� ,�

The origin text is:
Special character: Ä,ä,Ö,ö,Ü,ü,ß,€

Can you help me now please?

high_flyer
26th November 2010, 14:38
You need to know from which encoding to which you need to convert, and it seems you know how to convert between encodings, so what is the problem you have?

whitefurrows
26th November 2010, 16:00
The encoding for the files is known, but i don't know how can i use the coding to read the file. I need to encode at this place char c = entire[i]; Ther is no problem to use QChar if that mouch more easy to handle! You understand my problem?

high_flyer
26th November 2010, 16:42
You understand my problem?
Not really.
Why don't you just do exactly as shown in QTextCodec documentation?
If you know the file encoding, then I still fail to understand your problem, unless your encoding is not supported by QTextCodec.
From the docs:


QByteArray encodedString = "..."; //from the file
QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
QString string = codec->toUnicode(encodedString);

mattc
26th November 2010, 18:50
You may want to replace the QIODevice with a QTextStream and then use QTextStream::setCodec