Search string in C text file

The following code reads a text file one character at a time and prints it to standard output:

#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 typing on stdout [putchar (ch)], I want to search for a file for specific lines provided in another text file, i.e. strings.txt and output a string matching 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 first three lines will match text_file.txt. I did some research on file operations in C, and it seems that I can read one character at a time using fgetc[as in my code], one line c fgetsand one block c fread, but not a word, I suppose, would be perfect in my situation?

+5
4

, , . .

, . , . , , - .

, , - 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;
}

:

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;
+7

: fgetc(), getc(), getchar() , char. EOF , , , char.

"fgrep":

fgrep -f strings.txt text_file.txt > out.txt

- fgets(). (, gets() !)

return 0; ( C99 "return 0;", main()). , C99 - "int" "int main()" ( C99, 0 ). , .

, , . "strstr()" ( , , , ).

, , , , . , Boyer-Moore Knuth-Morris-Pratt (: Rabin-Karp, ).

+4

, .

, , , - , . , , , .

If this trivial algorithm is not enough (in your case, it probably is), there is a much more complex algorithm for finding several substrings in one cf Rabin-Karp buffer at the same time .

+2
source
cat strings.txt |while read x; do grep "$x" text_file.txt; done
+2
source

All Articles