0

I tried to write a Program to search word in a file, this is what I wrote but it do not work. I want that this Program reads the parts of text that is scaled in the spaces. When I run it and write "Hello" I does nothing and ends but I want it so that the output is:

Hello
coolcool
horse
miein

Hello
cookiecookie
horse
lol

Here is the code:

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

#define max 30

int main() {
    FILE *file;
    char c[max + 1];
    int x;
    char input[max + 1];
    char suche[max + 1];
    char output[max + 1];

    printf("Please write the word to search ");
    fgets(input, max, stdin);

    file = fopen("Testo.dat", "r");

    //here does the programm fail
    while (feof(file) != 0) {
        fgets(suche, max, file);
        if (strcmp(input, suche) == 0) {
            for (x = 0; x < 4; x++) {
                fgets(output, max, file);
                printf("%s", output);
            }
        }
    }

    fclose(file);
    return 0;
}

This is the file where is want to search the parts of text:

Hello
coolcool
horse
miein

Hello
cookiecookie
horse
lol

This
testetst
door
nicht

Thoes
breadbread
read
ja
chqrlie
  • 131,814
  • 10
  • 121
  • 189
zlSxrtig
  • 71
  • 6
  • 2
    did you debug your program either in a debugger or putting some printf statements in? – OldProgrammer Jan 06 '22 at 16:08
  • 2
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) - in your code it is especially wrong, as you have nested loops with `fgets` not checking `EOF` at all – Eugene Sh. Jan 06 '22 at 16:09
  • Minor: `fgets(input, max, stdin);` is sized 1 less than it can use. Use `fgets(input, max +1, stdin);` or even better: `fgets(input, sizeof input, stdin);` – chux - Reinstate Monica Jan 06 '22 at 17:36

1 Answers1

0

To simplify debugging, you should properly test for these conditions:

  • does fgets(input, max, stdin) successfully read a string from the user?
  • does fopen("Testo.dat", "r") successfully open the dictionary file?
  • does fgets(suche, max, file) successfully read a string from the dictionary? As a matter of fact, the reading loop should no test for feof(file) before attempting to read, it should just test if fgets() was successful.

A meaningful error message will help identify where the program fails.

Also note that you should pass the size of the destination array to fgets(), max + 1 instead of max. Since input is an array, just pass sizeof input.

Here is a modified version:

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

#define BUFFER_SIZE  32

int main() {
    char input[BUFFER_SIZE];
    char suche[BUFFER_SIZE];
    char output[BUFFER_SIZE];
    FILE *file;

    printf("Please write the word to search: ");
    if (!fgets(input, sizeof input, stdin)) {
        printf("input error\n");
        return 1;
    }
    file = fopen("Testo.dat", "r");
    if (file == NULL) {
        perror("Cannot open Testo.dat");
        return 1;
    }
    while (fgets(suche, sizeof suche, file)) {
        if (strcmp(input, suche) == 0) {
            for (int x = 0; x < 4; x++) {
                if (fgets(output, sizeof output, file))
                    printf("%s", output);
            }
        }
    }
    fclose(file);
    return 0;
}

Note also that fgets() reads a full line, including the trailing newline, so you will find a match only if the dictionary contains words without spaces.

chqrlie
  • 131,814
  • 10
  • 121
  • 189