How to search for a particular word in a text file and display the whole line of the corresponding text? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to search for a particular word in a text file and display the whole line of the corresponding text?

I am working on a project in which I need to include several search functions for the day, date, month, name etc. What I want is my program to be able to search for a particular name (for eg) and then display that whole record if the name is found. Similarly, if a user enters a particular date, I want my program to display the whole data for that particular date from the text file. I am currently new to using files in c and so am facing a bit difficulty in creating these search functions cuz I'm not sure which C library functions to use (eg fgets, fgetc, strstr). Your help is highly appreciated. Thanks!!

9th Dec 2018, 7:53 AM
Yusha Arif
Yusha Arif - avatar
1 Answer
+ 9
Read the whole line from text file using fgets function. Tokenize the whole line into substrings using strtok function. Compare each substring using strcmp function. If it matches, do whatever you need to do.
9th Dec 2018, 9:33 AM
blACk sh4d0w
blACk sh4d0w - avatar