Find in a file.

This is a discussion on Find in a file. within the C Programming forums, part of the General Programming Boards category; How can I find the number of lines in a file? What I want to do is load 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,796
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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, 11:21 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21