【问题标题】:How to search for words in a string in a text file?如何在文本文件的字符串中搜索单词?
【发布时间】:2016-06-04 18:11:10
【问题描述】:

我正在尝试将来自用户输入的单词与来自文本文件的字符串进行匹配。

当此代码运行时,它会在文件打开后崩溃。 (以****标记) 如何更改它以正确匹配用户输入的字符串与文本文件中的字符串。

任何帮助将不胜感激,谢谢。

const int Size = 81;                // 80 characters for the line + 1               for the '\0'
const int MaxNumberOfWords = 10;

int main() {
  char input[81], temp[81], fin[81];
  printf("Input a string\n");
  fgets(input, 81, stdin);
  int len = strlen(input);


  char *div;
  div = strtok(input, " ");
  while (div != NULL) {
    printf("%s\n",div);
    div = strtok(NULL, " ");
    
    
    ifstream inStream;                          // declare an input stream for my use
    char theWords[ MaxNumberOfWords][ Size];    // Array to store words from input line
    int wordRow = 0;                            // Row for the current word
    char wordToLookup[ Size];                   // word to lookup
    bool wordWasFound = false;                  // flag to track whether or not word is found
    char c;                                     // stores return character after input
    
    inStream.open( "C:\\Users\\dqiao4\\Desktop\\Dev-Cpp\\dictionaryMax6.txt");
    assert( ! inStream.fail() );  // make sure file open was OK
    
    
    //*****this is where the code crashes
    while ( inStream >> theWords[ wordRow]) {
      wordRow++;
    }
    
    

    for (int i=0; i<wordRow; i++)  {
      // See if this word matches
      if ( strcmp( div, theWords[ i]) == 0 ){
        wordWasFound = true;
        break;      // quit looking
      }
    }
  }
}

【问题讨论】:

  • 请展示您的研究成果。请先阅读How to Ask页面。
  • 但是您的具体问题是什么?如果您正在寻找某人为您编写代码,那么您来错地方了。请尝试自己编写代码。如果经过真正的努力后您仍然遇到困难,请展示您的代码并提出具体问题。
  • @thedude 强烈建议不要使用代码图片。我们应该如何将其复制并粘贴到我们的 IDE 中?
  • @thedude C 还是 C++?根据您选择的语言,您还会得到非常不同的答案。
  • 不要将代码作为图片发布。你还没有问过一个问题。您需要什么帮助?

标签: c++


【解决方案1】:

int main() {

char input[81];int i=0,j=0,k=0;
cout<<"Input a string ";
while(i<=80){
 input[i]=getch();
 cout<<input[i];
 if(input[i]=='.')
 break;      
 i++;
}
ifstream File("C:\\User\\New.txt");
string line;
if(File)
{   
    while(getline(File, line))
    {
        char buff[1024];
        strcpy(buff, line.c_str());
        while(j<35){
            k=0;
          while(k<i){
            if(buff[j]==input[k])
            {
                int j1=j,k1=k;
                while(true){
                  if(buff[j1]==input[k1])
                    {
                        if(input[j1]=='.'){
                            cout<<"match";
                            return 0;
                        }   
                        j1++;k1++;
                    }
                    else
                    break;
                }
          }k++; 
         }j++; 
        cout<<endl;  
    }
}

} }

【讨论】:

    【解决方案2】:
    #inlude <sstream>
    #include <string>
    

    在您的源代码中并读取字符串流缓冲区中的文本文件,将其转换为字符串并执行

    auto pos = file_in_str.find("word_to_find");
    

    那个pos是文件中单词的起始索引

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 2018-06-18
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多