【问题标题】:Search string in a file in c在c中的文件中搜索字符串
【发布时间】:2015-06-08 09:04:26
【问题描述】:

我正在尝试编写一个可以在文件(称为 student.txt)中搜索字符串的程序。我希望我的程序在文件中找到相同的单词时打印该单词,但显示错误。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
int num =0;
char word[2000];
char *string[50];

FILE *in_file = fopen("student.txt", "r");
//FILE *out_file = fopen("output.txt", "w");

if (in_file == NULL)
{
    printf("Error file missing\n");
    exit(-1);
}

while(student[0]!= '0')
{
    printf("please enter a word(enter 0 to end)\n");
    scanf("%s", student);


    while(!feof(in_file))
    {
        fscanf(in_file,"%s", string);
        if(!strcmp(string, student))==0//if match found
        num++;
    }
    printf("we found the word %s in the file %d times\n",word,num );
    num = 0;
}

return 0;
 } 

【问题讨论】:

  • if(!strcmp(string, student))==0 应替换为 if(!strcmp(string, student)==0)
  • 仍然出现错误
  • 你到底遇到了什么错误?
  • 您是在寻找字符串搜索还是单词搜索。例如,如果您正在搜索字符串“to”。文件内容为:。输出为 5。但实际上只有一个单词“to”

标签: c string search data-structures string-search


【解决方案1】:

添加了最简单形式的示例代码。照顾任何角落案例。 如果您正在搜索字符串“to”。文件内容为:

<tom took two tomatoes to make a curry> . 

输出为 5。但实际上只有一个单词“to”。

代码:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char const *argv[])
{
        int num =0;
        char word[2000];
        char string[50];
        char student[100] = {0};

        while(student[0]!= '0')
        {
                FILE *in_file = fopen("student.txt", "r");
                if (in_file == NULL)
                {
                        printf("Error file missing\n");
                        exit(-1);
                }

                printf("please enter a word(enter 0 to end)\n");
                scanf("%s", student);
                while ( fscanf(in_file,"%s", string) == 1)
                {
                        //Add a for loop till strstr(string, student) does-not returns null. 
                        if(strstr(string, student)!=0) {//if match found
                                num++;
                        }
                }
                printf("we found the word %s in the file %d times\n",student,num );
                num = 0;
                fclose(in_file);
        }
        return 0;
}

正如我的同事所说的那样,我们需要再有一个循环来遍历同一行中相同单词的任何进一步实例。

注意:如果您只想计算单词“to”,请确保检查所有可能的单词分隔符的“string - 1”和“string + 1”字符像空格、逗号、句号、换行符、感叹号、与号、等于和任何其他可能性。一种简单的方法是使用 strtok 根据参数中指定的分隔符将缓冲区标记为单词。查看如何使用 strtok。

http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

【讨论】:

  • 谢谢,但是很抱歉在这里混淆了东西,我正在寻找这个词。比如,你提到的例子我想让我的程序找到并打印'to'。
  • 您肯定需要使用strtok,或以其他方式解析单词。如所写,您的代码每行只计算一次。
【解决方案2】:

您应该从文件中读取(创建函数)单词。我所说的单词是指由空格(例如空格)包围的非空格字符数组(但不要在单词列表中记录空格)。然后搜索单词列表(或通过动态单词)以查找所需的单词。

【讨论】:

    【解决方案3】:

    在最后一行 printf() 中使用变量 student 或将匹配的文本放入变量 word 并检查您的 if 条件。

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 2011-01-31
      • 2010-12-10
      • 2012-10-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多