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?