Here's a couple of ways to do it.
This first way is looking for numbers specifically.
Code:
#include <stdio.h>
#define SEARCH_FOR_NUM 10
int main(void)
{
FILE *fp;
int i, j;
if ((fp = fopen ("test.txt", "r")) == NULL)
{
perror ("test.txt");
return 1;
}
while ((j = fscanf(fp, "%d", &i)) != EOF)
{
if (j != 1)
{
fgetc(fp);
continue;
}
if (i == SEARCH_FOR_NUM) printf ("Found it!\n");
}
fclose (fp);
return 0;
}
Or you could look for strings this way:
Code:
#include <stdio.h>
#include <string.h>
#define SEARCH_FOR_STR "10"
int main(void)
{
FILE *fp;
char buf[1024]; /* choose appropriate size */
if ((fp = fopen ("test.txt", "r")) == NULL)
{
perror ("test.txt");
return 1;
}
while (fgets(buf, 1024, fp))
{
if (strstr(buf, SEARCH_FOR_STR))
printf ("Found it!\n");
}
fclose (fp);
return 0;
}