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