Changing how 'main' parameters are interpreted

This is a discussion on Changing how 'main' parameters are interpreted within the C Programming forums, part of the General Programming Boards category; Hi everyone, I've written a function that searches a file for a string. The way it works is that I ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Changing how 'main' parameters are interpreted

    Hi everyone,

    I've written a function that searches a file for a string. The way it works is that I type in the function name and then the arguments are the string and filename. For example when I execute the program I'll do something like:

    ./program joe filename.txt

    where program is my executable program
    joe is the string to be found
    filename.txt is the file to be searched

    However, if I put ( for the string to be found, the result is a message displayed at execution time saying: "Too many ('s."

    Does anyone know how I can modify my program so that if I put ( as the string to be searched it'll search for the character ( rather than interpreting it as an unclosed parathesis?

    Note: I want to be able to write it as ( when I try to execute my program, it's not in the command line that I want to change it..

    Thanks,

    Canadian0469

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    No, because your program is never run. Your OS can't figure out what you are trying to do.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually, I think what is happening is Canadian's shell is intercepting the (, and doesn't know what to do with it; and they were hoping that they could fix that from within their program.

    The answer is that you can't. You'll have to escape the ( or put it in quotes or something, like this:
    Code:
    $ ./program \( file.txt
    $ ./program '(' file.txt
    Alternatively, of course, you could have an option where it reads a file for the pattern to search for. Then if you supported '-' as a synonym for stdin, you could use
    Code:
    $ ./program -f - file.txt
    (
    ^D
    $
    or something.

    I could have it all wrong and my idea that it is the shell could be mistaken, but that's my take on it.
    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
    Oct 2007
    Posts
    52
    Alright thanks for the quick replies guys.

    That's all I need to know. It's because my prof wanted to put ( as a test case but he didn't say what the output is supposed to be so I was wondering if the error is what he expects or not.

    I was thinking the same thing as you but just wanted to confirm my suspicions.

    Thanks again.

    Canadian0469

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  2. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  3. Defining main function!
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 02:00 PM
  4. why int main instead of void main?
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-26-2001, 10:49 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM

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