samedi 18 avril 2015

Search for a word in file line by line in C programming

I am trying to write code to search for a word in a file line by line and print the line containing the word. Functions fgets and getline didn't seem to work.



void FindWord(char *word , char *file){
char *line ;
line = (char*)malloc(1024) ;
int fd ;
fd = open(file , O_RDONLY );
while (fgets(line , sizeof(line) ,fd )!= NULL)
{
if (strstr(line , word )!= NULL)
{
printf("%s",line);
}
}
}

Aucun commentaire:

Enregistrer un commentaire