0

I have a simply task that I wish to do here is the workflow

  1. User inputs a string which he/she wishes to search exits in a text file
  2. If string is found, code then prints all the lines in a text file where that string exists.
  3. Code terminates/function ends.

Now I managed to get file reading working and all, but the issue happens when I combine user input.

For example, when the user inputs "sushi" it does not print out the lines of string in the text file where the word "sushi" exists.

But if I pass the term manually, it works fine (i.e strstr(lineOfText,"sushi));

Here is my code, what could the issue be

int main() {
    word_search();
    return 0;
}

int word_search() {
    FILE *textFile;
    char line[MAX_LINE_LENGTH];

    textFile = fopen("PATH TO TEXT FILE", "r");

    if (textFile == NULL) {
        return 1;
    }
    printf("Please input word to search:");
    char userInput[] = "";
    fgets(userInput, 250, stdin);

    while (fgets(line, MAX_LINE_LENGTH, textFile)) {
        if (strstr(line, userInput) != NULL) {
            printf("%s", line);
        }
    }

    fclose(textFile);
    return 0;
}

Contents of file

1 Wallaby Way Fenwick
1 Sushi Way Fenwick
1 Wallaby Sushi Way Fenwick
1 Alexandria Way Fenwick
1 Alexandira Sushi Ashfield Way Fenwick
Oka
  • 23,367
  • 6
  • 42
  • 53
  • C doesn't have a string class. The string class which C does not have is not `char`. You need to study how arrays work before using strings. Here's a newbie FAQ: [Common string handling pitfalls in C programming](https://software.codidact.com/posts/284849) – Lundin Feb 07 '23 at 07:23
  • `fgets()`, reading from `stdin` _leaves_ the `'\n'` on the end of the string. You won't find "Way", either, but will have success with "Fenwick" from the keyboard because that will be "Fenwick\n" in the buffer... `line[ strcspn( line, "\n" ) ] = '\0';` will fix your problem... (PS: That's a very peculiar filename.) (PPS: "sushi" will not match "Sushi"... Details matter...) – Fe2O3 Feb 07 '23 at 07:27

1 Answers1

1

The following

char userInput[] = "";

defines userInput to be an array of size 1. A buffer of size 1 can only hold the empty string.

Attempting to fill this buffer

fgets(userInput,250,stdin);

with up to 250 bytes will surely invoke Undefined Behaviour.


When a newline character is encountered, fgets stores it in the buffer (unless doing so would not leave room for the null-terminating byte).

You must remove the newline character from the query string. See: Removing trailing newline character from fgets() input.

An example program:

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

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "usage: %s FILENAME\n", *argv);
        return EXIT_FAILURE;
    }

    char needle[512];

    printf("Enter a string to search for: ");

    if (!fgets(needle, sizeof needle, stdin)) {
        if (ferror(stdin))
            perror("reading stdin");
        return EXIT_FAILURE;
    }

    needle[strcspn(needle, "\n")] = '\0';

    FILE *file = fopen(argv[1], "r");

    if (!file) {
        perror(argv[1]);
        return EXIT_FAILURE;
    }

    char line[1024];

    while (fgets(line, sizeof line, file))
        if (strstr(line, needle))
            printf("%s", line);

    fclose(file);
}

Running this program on its own source:

$ ./a.out find.c
Enter a string to search for: need
    char needle[512];
    if (!fgets(needle, sizeof needle, stdin)) {
    needle[strcspn(needle, "\n")] = '\0';
        if (strstr(line, needle))
Oka
  • 23,367
  • 6
  • 42
  • 53