QHash<QChar, Glyph*> glyph_map;
QFile data("input.txt");
if (!data.open(QFile::ReadOnly) {
// handle error and return...
}
QTextStream in(&data);
while (!in.atEnd()) {
QChar c;
in >> c;
// do stuff with your Glyph
glyph_map[c] = glyph;
}
If you do not have to use the STL containers, I would suggest using the Qt framework. Qt uses unicode by standard. Also, the classes are very well designed and feel really good to use.
You can create a QTextStream which will do the things that you want.
I think the following code should do.
QHash<QChar, Glyph*> glyph_map;
QFile data("input.txt");
if (!data.open(QFile::ReadOnly) {
// handle error and return...
}
QTextStream in(&data);
while (!in.atEnd()) {
QChar c;
in >> c;
// do stuff with your Glyph
glyph_map[c] = glyph;
}
请记住:在 Linux(也可能是 mac os x)上,您只需将 UTF-8 流输出到终端并获取正确的字符,在 Windows 中,这是一个完全不同的故事。
use while( !in ) instead of the eof variant, it's better, see this question
I'm assuming you're using Windows (as Linux and Mac normally have native UTF-8 platform encoding, which allows you to ignore most of this stuff).
What I would do is read the whole file as chars and convert it to wchar_t's using the handy functions in this question by me :).
Remember: on linux (and probably mac os x too) you can just output a UTF-8 stream to a terminal and get the right characters, in Windows, that's a whole different kond of story.
Unfortunately C++ is a bit lacking here - the w in wifstream refers to the types in use, rather than the ability to handle files with wide characters. You'll have to do some coding on your own, but you can find recipes at:
发布评论
评论(3)
如果您不必使用STL容器,我建议使用Qt框架。 Qt 按标准使用 unicode。此外,这些课程设计得非常好,使用起来感觉非常好。
您可以创建一个 QTextStream 它将执行您想要的操作。
我认为下面的代码应该可以。
If you do not have to use the STL containers, I would suggest using the Qt framework. Qt uses unicode by standard. Also, the classes are very well designed and feel really good to use.
You can create a QTextStream which will do the things that you want.
I think the following code should do.
使用
while( !in )
而不是eof
变体,效果更好,请参阅这个问题我假设您使用的是 Windows(因为 Linux 和 Mac 通常具有本机 UTF-8 平台编码,这允许您忽略大部分内容)。
我要做的是将整个文件读取为
char
并使用 这个问题是我的:)。请记住:在 Linux(也可能是 mac os x)上,您只需将 UTF-8 流输出到终端并获取正确的字符,在 Windows 中,这是一个完全不同的故事。
use
while( !in )
instead of theeof
variant, it's better, see this questionI'm assuming you're using Windows (as Linux and Mac normally have native UTF-8 platform encoding, which allows you to ignore most of this stuff).
What I would do is read the whole file as
char
s and convert it towchar_t
's using the handy functions in this question by me :).Remember: on linux (and probably mac os x too) you can just output a UTF-8 stream to a terminal and get the right characters, in Windows, that's a whole different kond of story.
不幸的是,C++ 在这里有点缺乏 - wifstream 中的 w 指的是正在使用的类型,而不是处理宽字符文件的能力。您必须自己进行一些编码,但您可以在以下位置找到食谱:
Unfortunately C++ is a bit lacking here - the w in wifstream refers to the types in use, rather than the ability to handle files with wide characters. You'll have to do some coding on your own, but you can find recipes at: