Thread: writing a grep program

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    writing a grep program

    Hi,
    This is not a homework problem. This is a sample test problem and I wanted to know how to do it. It would be great if someone can help me. The question is:

    Write a program, similar to grep, using regcomp(3C) and regexec(3C) to search for a regular expression pattern in standard input. Program synopsis:

    ./p1 pattern

    Simple search for regular expression pattern in standard input.

    Exit status:

    0 One or more matches were found
    1 No matches were found
    2 Syntax errors or inaccessible files (even if matches were found)

    Use regcomp flags: REG_NEWLINE | REG_NOSUB
    Use a function in the form:

    void regex_search( regex_t *preg, FILE *fp)

    to perform the search. This function will be invoked from the main program as:
    regex_t reg;
    ...
    regex_search( &reg, stdin);


    I did not undertsnd how to do it as I am not very familiar with C. I am from an IC design background. It would be a great help for my exam preparations, if somebody can help me with this.

    Thankyou very much.
    -Cnovice

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use fgets() to read each line from a file.
    Apply your regex matcher to each line.

    Lather, rinse and repeat until end of file.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use fgets() to read each line from a file.
    In C++? Really? I thought you were supposed to use getline or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    fgets() is part of the c++ language isn't it?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah, but it's like using printf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    and printf() is also part of the c++ language, although admittedly not commonly used in c++ programs

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, then, it's like using malloc(). Yeah, you can use it, but there are alternatives. (new, in the case of malloc.)

    (You're not supposed to use malloc() in C++ programs because it just allocates memory, it doesn't call constructors.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I did not undertsnd how to do it as I am not very familiar with C.
    I didn't read the board, I read this.
    Hence the 'C' answer.

    Moved to the 'C' board.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    Okay, then, it's like using malloc(). Yeah, you can use it, but there are alternatives. (new, in the case of malloc.)

    (You're not supposed to use malloc() in C++ programs because it just allocates memory, it doesn't call constructors.)
    but sometimes that's all you want anyway, especially when allocating arrays of PODs or structures that contain nothing but POD data objects. However, for consistency, I agree with you that malloc() and new should not be used in the same breath (program).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 09:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM