Thread: Find in a file.

  1. #1
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    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.
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User tidda's Avatar
    Join Date
    Mar 2002
    Posts
    37
    how does one search for text in a file ? Is there any function for that ?
    -tidda

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM