使用FileUtils读取txt文档,关于中文字符该怎么处理

现在我要做生成一个字典的操作
先采用FileUtils读取txt文档,文档内容为中文
FileUtils::getInstance()->getStringFromFile(“fonts/mytext.txt”);

然后for循环进行遍历操作,取出文档所使用的每一个单字存入map
代码如下:
vmap.clear();//清空map
std::string r_str = FileUtils::getInstance()->getStringFromFile(“fonts/mytext.txt”);//读取无序的数据库数据
std::string key_str = “”;
int keyId = 0;
//遍历获得的数据库字符串
for (int i = 0; i < r_str.size();i++)
{
//检测字符串中的字符
if (r_str.at(i) == ‘\r’)//如果字符==回车符
{
}
else if (r_str.at(i) == ‘\n’)//如果字符==换行符
{
}
else{//否则进行检测是否插入map
key_str = r_str.at(i);//获取字符
if (vmap.empty())//如果map为空,添加
{
vmap[StringUtils::format("%d", keyId)] = key_str;
keyId++;
}
else//否则,查找map中是否已经存在此字符
{
ValueMap::iterator it = vmap.end();
it = std::find_if(vmap.begin(), vmap.end(), vmap_value_finder(key_str));
if (it == vmap.end())//不存在,插入map
{
vmap[StringUtils::format("%d", keyId)] = key_str;
//log(“not found,insert new value,key=%d value=%s”, keyId, key_str.c_str());
keyId++;
}
else
{
//log(“found key:%s value:%s”, it->first.c_str(), it->second.asString().c_str());
}
}
}
}
但是实际操作时发现中文字符是是在字符串中是占用2个字符位置的,请问该怎么解决?

感觉是文件的编码方式有问题,改成utf8试试呢

是指的txt文件的编码格式?我试了试把txt文档设置成了utf-8的格式。然后读取时还是得,中文是占用字符串中的2个字符

代码里面utf8转utf16或者utf32再处理中文,处理完转回utf8
http://www.cplusplus.com/reference/locale/wstring_convert/