C++ 尝试读取文件并将某些字符串写入其他文件

C++ Trying to read through file and write certain strings to other file

本文关键字:文件 字符串 其他 读取 C++      更新时间:2023-10-16

dictionary.txt有以下单词:

apple
orange
pear
kiwi
fruit
orange
plum
banana

在主要:

cin >> userIn;
userIn = tolower(userIn);
stringCheck[loopCount] = userIn;
while (fileMain >> string1) {
  if (letterChecker(string1, stringCheck, loopCount) == 0){
    fileDupl << string1 << endl;
  }
}

在我的函数中:

int letterChecker(char string1[], char stringCheck[], int loopCount)
{
  int i = 0;
  for (i = 0; i<loopCount; i++){
    if (strchr(string1, stringCheck[i]) == NULL){
      return 0;
      break;
    }
  }
}

目标:我希望用户输入字符。如果该 txt 文件中的单词没有用户输入的实例,我希望将该单词写入不同的文本文件(例如:用户输入"a"。"Kiwi"、"fruit"和"plum"被写入另一个 txt 文件中,因为它们没有"a"(。 我已经弄清楚如何使用清除和查找函数重置流的错误标志和位置。但是,问题是当我运行此代码时,整个列表都会写入另一个txt文件中。它似乎完全忽略了该功能。你能帮助引导我朝着正确的方向前进吗?

只需在字母检查器函数的末尾添加一个 int 值的 return 语句。