C++如何在读取文件时忽略符号
C++ How can I ignore symbols when reading a file?
这是我当前读取的文件代码:
void Dictionary::processFile() {
ifstream fin;
fin.open("article.txt");
if (fin.fail( )) {
cout << "Input file opening failed.n";
exit(1);
}
string word;
while (!fin.eof()) {
fin >> word;
cout << word << endl;
}
cout << endl;
fin.close();
}
如何让我的代码忽略符号(".';:!等),只输出/读取单词?目前它正在读取文章上的每个符号。example"测试。","they"
像现在一样读取"单词",但在打印"单词"之前,先从字符串中过滤掉不需要的字符。
C++有许多算法函数可以帮助你做到这一点。出于您的目的,您可以查看例如std::remove_if
并执行类似的操作
static std::string const symbols = "".';:!";
while (fin >> word)
{
word.erase(std::remove_if(word.begin(), word.end() [symbols&](char const& ch) {
return std::any_of(symbols.begin(), symbols.end(), [ch](char const& sym) {
return ch == sym;
});
});
if (!word.empty())
{
// Do something with the "word"
}
}
如果您可以使用Boost.Iostrems,您可以为流编写自己的InputFilter。查看此处的详细信息http://www.boost.org/doc/libs/1_60_0/libs/iostreams/doc/tutorial/writing_filters.html
相关文章:
- 使用gcc从静态链接的文件中查找可选符号
- 检测 COFF 对象文件中C++内联符号
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 如何从二进制文件中的给定符号中获取调用程序图
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- CMake 生成的 MSVC 项目找不到符号,即使为其相关 dll 正确生成了 lib 文件也是如此
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 可以在 cpp 文件中C++定义为特殊符号的变量β
- 尝试将字符串从文件读取到无符号字符向量中
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- 体系结构x86_64的未定义符号:c++/ h 文件和 cpp 文件
- 错误LNK2019:未解析的外部符号.c文件到cpp
- Visual Studio 2012 性能分析无法加载符号文件
- 查看给定VC++ exe中的所有符号/文件的方法
- gdb:在0x2aaaaaaab000添加的符号文件系统提供的 DSO 中找不到可加载的部分
- gdb:在添加的符号文件系统提供的编号为0x#的DSO中找不到可加载的节
- 类型是否在最终二进制文件中公开?它们在符号文件和RTTI中可用吗?
- Oracle是否为OCCI/OCI提供公共符号文件(PDB)
- gdb:什么是用于调试的符号文件
- 非常困如何找到符号文件