C++ C++;如何在读取文本文件时忽略特定字符?

C++ C++;如何在读取文本文件时忽略特定字符?,c++,C++,我有一个主函数,可以读取文本文件 int count = 0; std::string fileName; std::fstream readFile; std::string storeFile; char myWord[50000]; int main(int argc, char *argv[]) { std::cout << "Please enter the name of the file: " << std::endl; //pr

我有一个主函数,可以读取文本文件

int count = 0;
std::string fileName; 
std::fstream readFile;
std::string storeFile;
char myWord[50000];

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

 std::cout << "Please enter the name of the file: " << std::endl; //prompts user for the filename
 std::cin >> argv[0];  //stores the filename is the first element of argv[]

fileName = argv[0];
readFile.open(fileName);

 if(!readFile) {
      std::cerr << "ERROR: failed to open file " << std::endl;  //if the file cannot be opened an error is displayed
      exit(0); //if it cannot open the console terminates
      } else {
          std::cerr << "File successfully opened" << std::endl;
      }

      while(readFile >> storeFile){
          if(readFile.bad()) {
              std::cerr << "File failed to read " << std::endl;
              break; //loop terminates
                } else {
                   for (int i = 0; i < sizeof(myWord)/sizeof(myWord[0]); i++){
                                  readFile >> myWord[i]; 
                                  count++;
                } 
    }

    readFile.close();

在读取文件时,我如何忽略puncuation?(因此,我可以选择符号“,:和)。

您可以选择临时字符,然后进行检查
charc;readFile>>c;如果(..)myWord[i]=c

您可以阅读行,用空格替换点状字符,然后使用istringstream和std::skipws阅读单词

只需删除每个单词的标点符号,然后忽略空单词?你试过这么做吗?C++库中没有内置函数,可以为您做。你必须自己编写代码才能做到这一点。@SamVarshavchik文本文件本身非常庞大,大约有5000个单词,因此我无法单独删除puncuation,如果这是你的建议,为什么不呢?是什么阻止了你这么做?@SamVarshavchik因为那是非常冗长和低效的为什么你认为它“非常冗长和低效”?我估计这只是两到三行代码。使用
std::remove_if
erase
storeFile
中删除所有标点符号的一行代码;然后
如果(storeFile.empty())继续。为什么你认为这将比其他方法效率低?<代码> > /Cord>在C++中性能不佳是臭名昭著的。例如,它必须为其持续时间构造一个异常处理程序。这样做,对于文件中的每个字符,一次一个?现在,这将是可怕的性能。我选择这个,因为我认为他需要一个最简单的方法(更像他的代码)。当然,如果我们有性能问题,我们必须采取其他方式
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.