c++ - 如何从我正在阅读的文件中删除所有标点符号?

标签 c++

这是我在 main.cpp 文件中的主要方法:

#include <iostream>
#include "ReadWords.h"
#include "Writer.h"
#include <cctype>
#include <string>

using namespace std;

int main() {

    int count;
    int length;
    string word;

    cout << "Please enter a filename: " << flush;
    char filename[30];
    cin >> filename;

这是我试图删除文件中所有标点符号的地方。目前我能够读取文件并打印所有值。我正在努力访问文件并删除标点符号。我知道我要使用 ispunct。我尝试了许多不同的实现,但无法使其正常工作。如何删除文件中的所有标点符号?

    ReadWords reader(filename);
    while (reader.isNextWord()){
        count = count + 1;
        reader.getNextWord();


    }

    cout << "There are: " << count << " words in this text file" << endl;    
    return 0;
}

这是另一个名为 ReadWords 的文件,其中包含我所有的方法声明:我认为这不相关/不需要

#include "ReadWords.h"
#include <cstring>
#include <iostream>
using namespace std;

void ReadWords::close(){

}
ReadWords::ReadWords(const char *filename) {
    //storing user input to use as the filename

        //string filename;

        wordfile.open(filename);

        if (!wordfile) {
            cout << "could not open " << filename << endl;
            exit(1);
        }

}

string ReadWords::getNextWord() {

    string n;
    if(isNextWord()){
        wordfile >> n;
        return n;

    }

}

bool ReadWords::isNextWord() {

        if (wordfile.eof()) {
            return false;
        }
        return true;
}

最佳答案

一次从输入文件中读取一个字符。

对于您阅读的每个字符:

  • 如果字符不是标点符号,则将其写入输出文件。
  • 如果字符是标点符号,则不要将其写入输出文件。

关于c++ - 如何从我正在阅读的文件中删除所有标点符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33696273/

相关文章:

c++ - 如何部署 C++ 应用程序并为我的音乐播放器创建安装文件

c++ - 在实现 iterator 和 const_iterator 类时避免代码重复的最佳实践

c++ - HP-UX 中对 FIFO 的轮询立即返回

c++ - 在布局上调整 QGraphicsView 小部件的大小

c++ - Visual Studio 跳过代码 mfc C++

c++ - C++ 中的多态性和虚函数

c++ - cppcheck 如何抑制内联不匹配抑制?

c++ - : Insert pair into multimap, 其中一种类型如何是抽象的?

c++ - 是否可以删除取消引用的指针

c++ - 5 检查的倍数