Hi guys

How do I search a file ".txt" which contains three lists side by side for a word ,
then print the line that the word I searched for is in?

NOTE:
-The lists are lists of names .

it would be beter if you show how to do it with 1-D or 2-D arrays .

thanks

Use fgets in a while loop. When fgets() returns NULL, then you have reached the end of the text file. Aha!

Inside that while loop, fgets() will put each line into your char array, which must be large enough to fit all the names into for ONE line. (for even the longest row, plus 1 for the end of string char which it adds).

Then, you use strstr() to search for the target name, within your char array. If it returns a valid pointer (instead of NULL), then your name is in the char array.

The char array can be just one dimension, since each line from the file, will be handled sequentially.

If you want show - so do I. Put some skin into this assignment, and post your code to try this, and tell us what your problem is.

DON'T just say "Please fix this, it doesn't work". Because that doesn't work for us. We want to help, not be your homework bitch. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.