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

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

问题描述

以下code读取一个文本文件,在当时一个字符并将其打印到stdout:

 的#include<&stdio.h中GT;诠释的main()
{
    烧焦file_to_open [] =text_file.txt,CH;
    FILE * file_ptr;    如果((file_ptr = FOPEN(file_to_open,R))!= NULL)
    {
        而((CH =龟etc(file_ptr))!= EOF)
        {
            的putchar(CH);
        }
    }
    其他
    {
        的printf(无法打开%s \\ n,file_to_open);
        返回1;
    }
    返回(0);
}

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

text_file.txt


1993年至1999年的奔腾
1997年至1999年奔腾II
2099至03年的Pentium III
1998 - 2009至强
2006至2009年的英特尔酷睿2

strings.txt


Nehalem处理器
AMD速龙
奔腾

在这种情况下, text_file.txt 的三个第一行会匹配。我已经做在C文件操作的一些研究,它似乎是我可以用龟etc 时读取一个字符[就像我在code做]与与fgets FREAD 一个块,但我的猜测是在我的处境完美的无字一行<? / p>

解决方案

我假定这是一个学习的过程,你只是在寻找一个地方开始。否则,你不应该推倒重来。

下code应该给你的所涉及的想法。这是一个程序,允许您指定要搜索文件的名称和一个参数在该文件中进行搜索。你应该能修改此把短语在字符串数组搜索和检查是否有任何的数组中的话出现在任何线路的读取。

您正在寻找的是 的strstr

 的#include&LT;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;#IFDEF DEBUG
#定义INITIAL_ALLOC 2
#其他
#定义INITIAL_ALLOC 512
#万一字符*
read_line(FILE * FIN){
    字符*缓冲区;
    字符* tmp目录;
    INT read_chars = 0;
    INT BUFSIZE = INITIAL_ALLOC;
    字符*线=的malloc(BUFSIZE);    如果(!行){
        返回NULL;
    }    缓冲=行;    而(与fgets(缓冲区,BUFSIZE - read_chars,翅)){
        read_chars = strlen的(线);        如果(行[read_chars - 1] =='\\ n'){
            行[read_chars - 1] ='\\ 0';
            回线;
        }        其他{
            BUFSIZE = 2 * BUFSIZE;
            TMP = realloc的(行,BUFSIZE);
            如果(TMP){
                行= tmp目录;
                缓冲=行+ read_chars;
            }
            其他{
                免费(线);
                返回NULL;
            }
        }
    }
    返回NULL;
}INT
主(INT ARGC,CHAR *的argv []){
    FILE *鳍;
    字符*线;    如果(argc个!= 3){
        返回EXIT_FAILURE;
    }    鳍= FOPEN(的argv [1],R);    如果(FIN){
        而(行= read_line(FIN)){
            如果(的strstr(线,的argv [2])){
                fprintf中(标准输出,%S \\ n,行);
            }
            免费(线);
        }
    }    FCLOSE(翅);
    返回0;
}

示例输出:


E:\\ TEMP> searcher.exe searcher.c字符
字符*
    字符*缓冲区;
    字符* tmp目录;
    INT read_chars = 0;
    字符*线=的malloc(BUFSIZE);
    而(与fgets(缓冲区,BUFSIZE - read_chars,翅)){
        read_chars = strlen的(线);
        如果(行[read_chars - 1] =='\\ n'){
            行[read_chars - 1] ='\\ 0';
                缓冲=行+ read_chars;
主(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\n", 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] == '\n' ) {
            line[read_chars - 1] = '\0';
            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\n", 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] == '\n' ) {
            line[read_chars - 1] = '\0';
                buffer = line + read_chars;
main(int argc, char *argv[]) {
    char *line;

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

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