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?

-The lists are lists of names .

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


7 Years
Discussion Span
Last Post by Adak

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. ;)

Edited by Adak: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.