【问题标题】:My program not work as expected我的程序没有按预期工作
【发布时间】:2015-04-23 14:42:11
【问题描述】:

我需要找到一些字符串的位置。这些字符串存储在一个名为 queryfile 的文件中,来自另一个名为 datafile 的文件。

但是,我的程序没有按预期运行。 有人可以帮我吗?

我的程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    FILE *queryfile;
    queryfile = fopen("op2query.txt","r");

    FILE *datafile;
    datafile = fopen("op2data.txt","r" );

    int i = 1;
    char word[99];
    char search[99];

    if(queryfile==NULL) {
      printf("Error in reading Query File");
      exit(1);
    }
    if(datafile==NULL) {
      printf("Error in reading Data File");
      exit(1);
    } 

     while(fscanf(queryfile,"%98s",search)==1){  
         while(fscanf(datafile,"%98s",word)==1){
             if (strcmp(word,search)==0){
                 printf("\n %i %s ", i, search);
                 rewind(datafile);
                 i=1;
                 break;
              }
              else 
                 i++;   
         }
     }

    fclose(datafile);
    fclose(queryfile);
    return 0;
    }

【问题讨论】:

    标签: c file-io


    【解决方案1】:

    我通过将查询字符串拆分为单词来构建每组要测试的单词的数组。这些词可以跨越数据文件中的换行符。我将数据文件位置标记在集合的第二个单词上,如果搜索失败,我会寻找该点(如有必要)。即使我复制数据文件中的每个单词“age”,程序也会成功。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXWORDS    5
    #define MAXLEN      99
    
    int main()
    {
        int j, i, done, words, count;
        long mark;
        char word[MAXLEN];
        char search[MAXLEN];
        char *tok, *sptr[MAXWORDS];
        FILE *queryfile;
        FILE *datafile;
    
        if ((queryfile = fopen("op2query.txt","r")) == NULL) {
             printf("Error in reading Query File");
             exit(1);
        }
        if ((datafile = fopen("op2data.txt","r" )) == NULL) {
             printf("Error in reading Data File");
             exit(1);
        } 
    
        while(fgets(search, MAXLEN, queryfile) != NULL){
            words = 0;
            done = 0;
            count = 0;
            mark = -1;
            tok = strtok(search, " \r\n");
            while (tok && words < MAXWORDS) {       // build array of query
                sptr[words++] = tok;
                tok = strtok(NULL, " \r\n");        // strips newline too
            }
            if (words < 1)                          // no more queries
                break;
            rewind(datafile);                       // beginning of file
    
            while (!done) {                         // until none to read
                count++;
                if (mark >= 0)                      // when more than one word to search
                    fseek (datafile, mark, SEEK_SET);
                mark = -1;
                for (j=0; j<words; j++) {
                    if (j == 1)                     // mark for next search
                        mark = ftell(datafile);
                    if (fscanf(datafile, "%98s", word) != 1){
                        done = 1;                   // end of file
                        break;
                    }
                    if (strcmp(sptr[j], word)!=0)
                        break;                      // failed multi word search
                }
                if (done) 
                    printf("NOT FOUND!");
                else if (j == words) {              // if all words found
                    printf("%d", count);
                    done = 1;                       // success
                }
            }
            for (i=0; i<words; i++)
                printf(" %s", sptr[i]);             // show array of words asked
            printf("\n");
        }
        fclose(datafile);
        fclose(queryfile);
        return 0;
    }
    

    程序输出:

    18 wisdom
    40 season
    NOT FOUND! summer
    22 age of foolishness
    

    UPDATE - 找不到查询时我打印NOT FOUND!。在查询文件中添加了“summer”。

    【讨论】:

    • 非常感谢!!!但是如果我想告诉用户他们想要查找的查询不在文本中,我该怎么办。假设winter 在查询文件中,我想显示NOT FOUND! winter。非常感谢!
    • @tatatatat 看看。
    【解决方案2】:

    您应该在 fscanf 调用后面放置一些调试输出(例如 printf("search:&lt;%s&gt; word:&lt;%s&gt;", search, word);

    然后你会看到,fscanf 停止寻找空白。您将wisdom 与 op2data.txt 中的每个连续单词进行比较。

    您应该逐行阅读 fgets() 从搜索中删除 CR/LF。

    但请注意,数据文件中的多词搜索词可能会在各行之间拆分。喜欢:

    find me
    
    i am the text to find
    me in this file
    

    所以更好的解决方案是:

    • 逐行读取搜索字词(删除 CR/LF)(通过删除双空格和非字母对其进行规范化)
    • 从数据文件中读取一个块并对其进行规范化。
    • 比较或继续通过将数据中的读取位置移动搜索词长度的长度来继续

    【讨论】:

    • 我尝试使用 fgets,但屏幕上什么也没有显示
    • 再次尝试使用 fgets() 并让您按照我的建议显示结果;直接在阅读后面,在if之外
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多