对于配置文件,不同客户保存的编码也不一致,直接使用QFile 读取,很可能会出现乱码情况,解决方法就是使用类QTextStream 来读取每行数据,先看下QTextStream 帮助文档上的解释:
Internally, QTextStream uses a Unicode based buffer, and QTextCodec is
used by QTextStream to automatically support different character sets.
By default, QTextCodec::codecForLocale() is used for reading and
writing, but you can also set the codec by calling setCodec().
大概意思就是QTextStream 会自动支持不同的字符集,所以直接用这个类,还不用自己每个编码去转化,简单方便。
QString filePath = "../config.txt";
QFile file(filePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream textStream(&file);
while