I've been given an assignment to write a program in C that can open a txt file and search for a string. It must then print the lines in which the string was found.
I have the following code so far:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
main()
{
int i=0;
int line=1;
char s1[8]="Networks";
char s2[8]="networks";
char c,a[8];
FILE *fpt;
if((fpt=fopen("C:\\Keimeno.txt","r"))==NULL)
printf("\nERROR - cannot open the file ");
else
while ((c=fgetc(fpt))!=EOF)
{
if (c=='\n')
line=line+1;
a[i]=c;
i++;
if(i==8)
{
i=0;
if((strcmp(a,s2)==1)||(strcmp(a,s1)==1))
printf("The string was found in line %d...\n\n",line);
}
}
system("pause");
}
I would really appreciate it if someone helped me with this.
