Quick C question..

Discussion in 'Mac Programming' started by mmmdreg, Nov 3, 2006.

  1. mmmdreg macrumors 65816


    Apr 14, 2002
    Sydney, Australia
    I have a uni assignment where you have to do stuff to files in C.. Currently I have it starting, the getting the filename from stdin, the doing the stuff..which all works fine..

    Unfortunately, rather than putting the filename in after the program starts, I have to do it in the form "./program fileName1 fileName2" ..

    Can someone please tell me briefly how to pass.. parameters to a program like that?


  2. iMeowbot macrumors G3


    Aug 30, 2003
    Check out the recent argv issues thread. You'll want to do the same, skipping the atoi() business.
  3. mmmdreg thread starter macrumors 65816


    Apr 14, 2002
    Sydney, Australia
    worked perfectly. Thanks heaps.
  4. Soulstorm macrumors 68000


    Feb 1, 2005
    It would really help if you were familiar with the command line (Terminal). You see, many programs you invoke to be opened from the terminal can accept some arguments. The general rule for invoking a program with arguments is this:

    path_to_program argument1 argument2 .. and so on. Those arguments I described will be those files you want.

    Each program in C++ command-line can accept command line arguments. See this main:

    int main(int argc, char* argv[])

    Argc is the number of the arguments passed when invoking the program. Argv is a two dimensional array that holds those arguments. Every program has at least one argument passed. And that's it's name, accessed from argv[0], so the arguments you are going to pass manually (the files, that is) are going to be processed by accessing the argv[1] and argv[2] and... so on.

    I hope I gave you a good headstart. If you have any problems, I will tell you more, but not yet, since reading any official documentation would prove more valuable for you than my advice ;)

Share This Page