c++ - ispunct() 不检测单引号字符

标签 c++

我正在尝试读入文件并从文件中删除所有标点符号。我一直在使用 ispunct() 遍历字符串并检查字符是否是标点符号,但它似乎没有捕捉到所有标点符号。我想知道我是否做错了什么。这是我的代码:

2.txt

你好吗?

我很好,谢谢。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

//removes punctuation, numbers, and extra spaces
void removeNonAlph(string &tmp)
{
     for(int i = 0; i < tmp.length(); i++)
     {
         if (ispunct(tmp[i]))
             tmp.erase(i--, 1);
         else if (isdigit(tmp[i]))
             tmp.erase(i--, 1);
         else if ((tmp[i] == ' ') && (tmp[i+1]) == ' ')
             tmp.erase(i--, 1);
     }
 }

int main(int argc, const char * argv[]) 
{

    ifstream file("2.txt");
    string tmp;
    string words[500];

    while (getline(file, tmp))
    {
        removeNonAlph(tmp);
        toLower(tmp);
        cout << tmp << endl;
    }

    file.close();
}

输出:

你好吗

我很好,谢谢

最佳答案

(评论已移至答案,以便 future 的读者轻松发现)

当心编辑器将非 ASCII 引号放入您的文本文件中。许多编辑器通过使用不同的非 ASCII 字符代码以不同方式显示右引号和左引号来生成看起来更好的“智能引号”。 ispunct 通常只适用于 ASCII 输入。

关于c++ - ispunct() 不检测单引号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45973529/

相关文章:

C++ 输入未正确阻塞

c++ - 如何在C++ Lambda中捕获 vector 中的元素?

C++ 将 shared_ptr boost 为 hash_map 键

c++ - 读取目录中的多个文件以编辑和写入另一组文件

c++ - 如何在不将它们作为参数传递的情况下获取调用者的文件名和行号?

c++ - C strings vs const char* 让我感到困惑......请帮忙

c++ - 清除 ncurses 中的字段缓冲区

c++ - 如何将调试信息保存到 qt 中的特定文件中?

c++ - 我应该将 C++11 emplace_back 与指针容器一起使用吗?

c++ - Qt 与 QAMQP/RabbitMQ