在文本文件 C 中搜索字符串 [英] Search for string in text file C

查看:22
本文介绍了在文本文件 C 中搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码每次读取一个文本文件一个字符并将其打印到标准输出:

#include int main(){char file_to_open[] = "text_file.txt", ch;文件 *file_ptr;if((file_ptr = fopen(file_to_open, "r")) != NULL){while((ch = fgetc(file_ptr)) != EOF){putchar(ch);}}别的{printf("无法打开 %s
", file_to_open);返回 1;}返回(0);}

但不是打印到标准输出 [putchar(ch)] 我想在文件中搜索另一个文本文件中提供的特定字符串,即.strings.txt 并将匹配的行输出到 out.txt

text_file.txt:

<前>1993 - 1999 奔腾1997 - 1999 奔腾 II1999 - 2003 奔腾 III1998 - 2009 至强2006 - 2009 英特尔酷睿 2

strings.txt:

<前>尼哈勒姆AMD速龙奔腾

在这种情况下,text_file.txt 的前三行将匹配.我对C中的文件操作做了一些研究,似乎我可以用fgetc一次读取一个字符[就像我在我的代码中所做的那样],一行用fgets 和一个带有 fread 的块,但我认为在我的情况下没有一个词是完美的?

解决方案

我假设这是一个学习练习,而您只是在寻找一个起点.否则,您不应该重新发明轮子.

下面的代码应该让您了解所涉及的内容.它是一个程序,允许您指定要搜索的文件名和在该文件中搜索的单个参数.您应该能够修改它以将要搜索的短语放在字符串数组中,并检查该数组中的任何单词是否出现在任何读取的行中.

您正在寻找的关键功能是strstr.

#include #include #include #ifdef 调试#define INITIAL_ALLOC 2#别的#define INITIAL_ALLOC 512#万一字符 *read_line(FILE *fin) {字符*缓冲区;字符 *tmp;int read_chars = 0;int bufsize = INITIAL_ALLOC;char *line = malloc(bufsize);如果(!行){返回空;}缓冲区 = 行;while ( fgets(buffer, bufsize - read_chars, fin) ) {read_chars = strlen(line);if ( line[read_chars - 1] == '
' ) {行[read_chars - 1] = '';回线;}别的 {缓冲区大小 = 2 * 缓冲区大小;tmp = realloc(line, bufsize);如果 ( tmp ) {线 = tmp;缓冲区 = 行 + read_chars;}别的 {自由行);返回空;}}}返回空;}整数main(int argc, char *argv[]) {文件 *鳍;字符 * 行;如果(argc!= 3){返回 EXIT_FAILURE;}fin = fopen(argv[1], "r");如果(鳍){while ( line = read_line(fin) ) {if ( strstr(line, argv[2]) ){fprintf(stdout, "%s
", line);}自由行);}}关闭(鳍);返回0;}

示例输出:

<前>E:Temp> searcher.exe searcher.c 字符字符 *字符*缓冲区;字符 *tmp;int read_chars = 0;char *line = malloc(bufsize);while ( fgets(buffer, bufsize - read_chars, fin) ) {read_chars = strlen(line);if ( line[read_chars - 1] == ' ' ) {行[read_chars - 1] = '';缓冲区 = 行 + read_chars;main(int argc, char *argv[]) {字符 * 行;

The following code reads a text file one character at the time and print it to stdout:

#include <stdio.h>

int main()
{
    char file_to_open[] = "text_file.txt", ch;
    FILE *file_ptr;

    if((file_ptr = fopen(file_to_open, "r")) != NULL)
    {
        while((ch = fgetc(file_ptr)) != EOF)
        {
            putchar(ch);
        }
    }
    else
    {
        printf("Could not open %s
", file_to_open);
        return 1;
    }
    return(0);
}

But instead of printing to stdout [putchar(ch)] I want to search the file for specific strings provided in another textfile ie. strings.txt and output the line with the match to out.txt

text_file.txt:

1993 - 1999 Pentium
1997 - 1999 Pentium II
1999 - 2003 Pentium III
1998 - 2009 Xeon
2006 - 2009 Intel Core 2

strings.txt:

Nehalem
AMD Athlon
Pentium

In this case the three first lines of text_file.txt would match. I have done some research on file operations in C, and it seems that I can read one character at the time with fgetc [like I do in my code], one line with fgets and one block with fread, but no word as I guess would be perfect in my situation?

解决方案

I am assuming this is a learning exercise and you are simply looking for a place to start. Otherwise, you should not reinvent the wheel.

The code below should give you an idea of what is involved. It is a program that allows you to specify the name of file to be searched and a single argument to search in that file. You should be able to modify this to put the phrases to search for in an array of strings and check if any of the words in that array appear in any of the lines read.

The key function you are looking for is strstr.

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

#ifdef DEBUG
#define INITIAL_ALLOC 2
#else
#define INITIAL_ALLOC 512
#endif

char *
read_line(FILE *fin) {
    char *buffer;
    char *tmp;
    int read_chars = 0;
    int bufsize = INITIAL_ALLOC;
    char *line = malloc(bufsize);

    if ( !line ) {
        return NULL;
    }

    buffer = line;

    while ( fgets(buffer, bufsize - read_chars, fin) ) {
        read_chars = strlen(line);

        if ( line[read_chars - 1] == '
' ) {
            line[read_chars - 1] = '';
            return line;
        }

        else {
            bufsize = 2 * bufsize;
            tmp = realloc(line, bufsize);
            if ( tmp ) {
                line = tmp;
                buffer = line + read_chars;
            }
            else {
                free(line);
                return NULL;
            }
        }
    }
    return NULL;
}

int
main(int argc, char *argv[]) {
    FILE *fin;
    char *line;

    if ( argc != 3 ) {
        return EXIT_FAILURE;
    }

    fin = fopen(argv[1], "r");

    if ( fin ) {
        while ( line = read_line(fin) ) {
            if ( strstr(line, argv[2]) ){
                fprintf(stdout, "%s
", line);
            }
            free(line);
        }
    }

    fclose(fin);
    return 0;
}

Sample output:

E:Temp> searcher.exe searcher.c char
char *
    char *buffer;
    char *tmp;
    int read_chars = 0;
    char *line = malloc(bufsize);
    while ( fgets(buffer, bufsize - read_chars, fin) ) {
        read_chars = strlen(line);
        if ( line[read_chars - 1] == '
' ) {
            line[read_chars - 1] = '';
                buffer = line + read_chars;
main(int argc, char *argv[]) {
    char *line;

这篇关于在文本文件 C 中搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆