Find in a file.
How can I find the number of lines in a file?
What I want to do is load a file into a char**. In order to initialize the number of pointers to char I need to know the number of lines in the file. If someone knows an easier way I would appriciate the help.
Just allocate some space to start out with and if you reach the end of the block then realloc more space to handle the input.
how does one search for text in a file ? Is there any function for that ?
One writes a function :)
There is no standard function to do this, ( I am assuming that you are refering to a function that would perform the same sort of functionality as the UNIX command line tool grep?).
In the past I have written a simple interface for a couple of functions that perform pattern matching on a text file returning information such as line number, number of occurences etc. It makes heavy use of the standard functions such as strcmp() etc - but provides greater flexibility and more options.
Basically the operation is to open the file, read in a portion of the file, test this string/input to see if the "key" is contained within, repeat until the end of file is reached then close the file.
There is no need to store each line, if all you want to do is count them.
It's just fgets in a while loop and a counter