Getting parameters to "main" function... C programming using Xcode

Discussion in 'Mac Programming' started by s7on3d, Jan 12, 2008.

  1. s7on3d macrumors newbie

    Joined:
    Jun 9, 2007
    #1
    Hi everyone,
    I'm pretty new to my mac, and also relatively new to programming... First year engineering student :)
    I've written the following program as a command line program in C with Xcode:

    int main(int argc, char *argv[]) // MAIN FUNCTION!!
    {
    int i;
    for (i=0;i<argc;i++)
    {
    printf("argv[%d]: %s\n",i,argv);
    }
    return 0;
    }

    Which should receive input as a set of strings from the command line when the program is run, then print it back out.

    My question is how do I run the program (I assume from the terminal...) and pass on the parameters (in this case just a sentence)?

    After quite a bit of playing around I've managed to successfully reach the right directory in the terminal, and open the program (using the "open" command), but I can't work out how to send it the parameters...

    Thanks in advance,
    Ilan
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You shouldn't use open. That will run the program via launch services and is unlikely to preserve command line arguments. You should simply do what you would do on any other Unix system: supply the full path to the command you want to execute (in this case your program). If you are in the same folder as the executable then ./<your program name> will run the program.
     
  3. s7on3d thread starter macrumors newbie

    Joined:
    Jun 9, 2007
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    Any book on Unix will teach you all you need to know.
     

Share This Page