Thread: main () command line arguments

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    main () command line arguments

    Hey everyone. I'm a student at NIU and am looking for a little help to start me off on my first assignment. I just recently changed to a comp sci major, and am new to C++, but I have taken a course in C a few years back under my old major. Anyway, here's my question:

    For my assignments, I'm supposed to read in 2 filenames from a command line. My teacher gave me the syntax for the main() function as:

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

    I understand that argc is the number of arguments. The command line I will be entering is:

    programname lines.dat township.dat

    First, will argc = 2 or will argc = 3, and therefore will the
    char *argv[] look like this:

    argv[0] = programname or this argv[0] = lines.dat
    argv[1] = lines.dat argv[1] = township.dat
    argv[2] = township.dat

    Also, when I enter the above command line, does argc automatically get set to 2 or 3, and does argv get set to one of the two assignments i wrote above, or do I have to code that into the program.

    I am programing in UNIX, v. 5.8 and and using the g++ compiler.

    Any help would be greatly appreciated to help me start off. Thanks for reading

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Doesnt hurt to try and see what happens.

    Compile this code and run progname something somethingelse and see what happens.

    Code:
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    { 
    	cout << "argc = " << argc << endl;
    	for (int x=0; x < argc; x++)
    		cout << "argv[" << x <<"] = " << argv[x] << endl;
    
      return  0;
    }

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Do a search before starting a new thread

    This topic has been covered many times, also good luck with your c++-studies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many arguments to function int main?
    By plain in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2006, 06:01 PM
  2. Actient main
    By swgh in forum C Programming
    Replies: 2
    Last Post: 03-21-2006, 01:33 PM
  3. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  4. Passing arguments to main?
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 03:59 AM
  5. arguments passed to main?!?
    By threahdead in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2003, 08:43 PM