C++ command line parameters

Discussion in 'Mac Programming' started by jmarauder, Sep 27, 2009.

  1. jmarauder macrumors newbie

    Joined:
    Mar 26, 2009
    #1
    Hi all, I have a program for my data structures class due this week, and my instructor has said that the program needs to be able to run from the command line with a string of parameters that needs to be parsed.

    Normally all my programs that I have done up to this point have all prompted the user to input the file name for the program's input and generally for every other user defined variables. I have been doing hours of research trying to figure this out for myself, but can not seem to get it done for the life of me.

    I know I have written a good function to parse the string by testing it with string I have defined, but I can not seem to get the program to get the string parameter from the command line. I always get this error when i run this command

    any help would be awesome guys!
     
  2. electroshock macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #2
    What is the source code of the file called 'sr'? And what is the compiled binary's permissions? (i.e., ls -l sr)

    Is 'sr' a shell script or is it a compiled binary?
     
  3. jmarauder thread starter macrumors newbie

    Joined:
    Mar 26, 2009
    #3
    sr is the actual c++ file (sr.cpp)
     
  4. electroshock macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #4
    Okay, you may have at least two issues:

    1) The compiled sr binary may not have executable permissions and/or you may not be doing ./sr <command line arguments>

    2) In the source code, are you parsing argc/argv? E.g., see:

    http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

    If you'd like to get better-targeted suggestions, you may want to post the source code snippets or at least the part that handles CLI argument parsing.
     
  5. jmarauder thread starter macrumors newbie

    Joined:
    Mar 26, 2009
    #5
    here is my terminal window of when I am trying to run the program...

    does that provide any insight into the problem, or should I post the code snippets?
     
  6. electroshock macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #6
    That's helpful information -- thanks. Means that when you told Terminal to run a program called 'sr' in the current directory, it couldn't find that. So it spit out the error.

    To find out where the 'sr' binary might be, you could try this Terminal command:

    Code:
    find ~ -name sr
    Then cd to whatever directory it mentions then redo the ./sr <cli args> command
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Did you compile sr.cpp? Because if you didn't, nothing else will work.

    In the directory where sr.cpp resides, enter the following command and post the output:

    Code:
    ls -la
    
     
  8. jmarauder thread starter macrumors newbie

    Joined:
    Mar 26, 2009
    #8
    and I have set the executable's working directory to the "Project 1 Answer" directory
     
  9. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    You need to cd into the directory where the actual executable resides (somewhere in the "build" directory), or enter the path to it relative from where your current working directory is.

    Also, separating the command line options with semicolons won't work as they have a special meaning to the shell.
     
  10. jmarauder thread starter macrumors newbie

    Joined:
    Mar 26, 2009
    #10
    when I navigate to the actual executable I still get
    error. I even tried it with just inputing the parameters seperated by commas instead of semicolons. Help please still!
     
  11. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #11
    Can you do a "ls -al" of the build folder; then try running the app; and paste the results here?
     
  12. jmarauder thread starter macrumors newbie

    Joined:
    Mar 26, 2009
    #12
    after "ls -al"
    try running program with just input file as parameter
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    The main problem here is that you don't understand how bash interprets commands.

    First, you must be in the same directory as the "sr" command you just built, or you must give a pathname to the command. If you're not in that directory, then typing plain 'sr' as the command will never work. Bash doesn't search your entire disk looking for commands. It only searches specific dirs, the ones listed in its PATH variable.

    Second, the way bash works, it does not automatically search the current directory. That means you must type the command as "./sr" for it to find the command. (If you're wondering why it's this way, it's for security reasons.)

    Third, after the "./sr" you must enter the command-line args in quoted form, otherwise bash will interpret the semicolons as delimiters between separate commands.

    To summarize:

    Code:
    cd where/your/command/is/located
    ./sr "input=example2.txt;search=532;replace=2320"
    
    Since you haven't explained what these parameters mean, you may have to change the pathname of example2.txt, possibly to a relative pathname like ../../example2.txt. That's just a guess because you haven't said what your program does with its command-line args. Or you could explain what the parameters mean.

    Finally, you need to use the 'ls' command, especially with the -l option, and understand what it's telling you. So far, you've posted at least two outputs from 'ls -l', and none of them contained an 'sr' command, yet this absence has gone unnoted.
     
  14. jmarauder thread starter macrumors newbie

    Joined:
    Mar 26, 2009
    #14
    the text file will be used as my input file for the project
     
  15. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #15
    From what I'm reading, I'm not even sure the OP is clear on the process of compiling to an executable binary from the C++ source, and then executing the binary file. I assume he is, but it's not clear.

    The first post suggests that the OP has been able to run programs in the past (programs that prompt the user for input), but the inability to run the executable from the shell (regardless of command-line parameters) seems to contradict this. Unless the previous runs were from within an IDE such as Xcode, and this is the first time running directly from a shell?

    I strongly recommend to the OP, stepping back a bit and playing with some simple code to specifically exercise your knowledge of handling command line parameters. Write a little "hello world" program that takes the parameters into argc and argv and print them out.

    Once you get that working, then plug it into the rest of your program.
     

Share This Page