这是我在 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/