C++如何通过忽略每行的第一个字符来读取 unicode 文件
C++ how to read from unicode files by ignoring first character of each line
考虑一个包含 Unicode 单词的文件,如下所示
آب
آباد
آبادان
如果从右到左阅读,第一个字符是" آ"。
我的第一个要求是逐行读取文件。这很简单。
第二个要求是从每行的第二个字符逐行读取文件。 结果必须是这样的
ب
باد
بادان
如您所知,有一些解决方案,如 std::substr 来满足第二个要求,但 Afaik std::substr 不能很好地与 Unicode 字符配合使用。
我需要这样的东西
std::ifstream inFile(file_name);
//Solution for first requirement
std::string line;
if (!std::getline(inFile, line)) {
std::cout << "failed to read file " << file_name << std::endl;
inFile.close();
break;
}
line.erase(line.find_last_not_of("nr") + 1);
std::string line2;
//what should be here to meet my second requirement?
//stay on current line
//ignore first character and std::getline(inFile, line2))
line2.erase(line.find_last_not_of("nr") + 1);
std::cout<<"Line= "<<line<<std::cout; //should prints آب
std::cout<<"Line2= "<<line<<std::cout; //should prints
inFile.close();
C++11
具有Unicode转换例程,但它们不是很用户友好。但是您可以使用它们制作更多用户友好的功能,如下所示:
// This should convert to whatever the system wide character encoding
// is for the platform (UTF-32/Linux - UCS-2/Windows)
std::string ws_to_utf8(std::wstring const& s)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> cnv;
std::string utf8 = cnv.to_bytes(s);
if(cnv.converted() < s.size())
throw std::runtime_error("incomplete conversion");
return utf8;
}
std::wstring utf8_to_ws(std::string const& utf8)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> cnv;
std::wstring s = cnv.from_bytes(utf8);
if(cnv.converted() < utf8.size())
throw std::runtime_error("incomplete conversion");
return s;
}
std::string remove_first_char(std::string const& utf8)
{
std::wstring ws = utf8_to_ws(utf8);
ws = ws.substr(1);
return ws_to_utf8(ws);
}
int main()
{
std::string utf8 = u8"آبادان";
std::cout << remove_first_char(utf8) << 'n';
}
输出:
بادان
通过转换为固定代码点 (UCS-2/UTF-32(,您可以使用普通字符串函数处理字符串。不过有一个警告。UCS-2
并不涵盖所有语言的所有字符,因此您可能需要使用std::u32string
并在UTF-8
和UTF-32
之间编写转换函数。
这个答案有一个例子:https://stackoverflow.com/a/43302460/3807729
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- C++:使用另一个字符将一个字符大写
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 将字符从一个字符串简单分配到另一个字符串不起作用
- 为什么第一个Dynamic_cast没有投射到基类?
- 将第一个n个字符复制到std :: string
- 一个奇怪的字符出现在我的字符数组的第一个
- 当一长串字符传递给第一个 cin 时,为什么不暂停第二个 cin 上的输入?
- 哪个字符是 c++ 中 4 个字符中的第一个
- 从 std::string 中提取(第一个)UTF-8 字符