int main arguments

This is a discussion on int main arguments within the C++ Programming forums, part of the General Programming Boards category; 3 more tutorials down I have a question though: Code: #include <fstream> #include <iostream> using namespace std; int main ( ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    int main arguments

    3 more tutorials down
    I have a question though:
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main ( int argc, char *argv[] )
    {
      if ( argc != 2 ) // argc should be 2 for correct execution
        // We print argv[0] assuming it is the program name
        cout<<"usage: "<< argv[0] <<" <filename>\n";
      else {
        // We assume argv[1] is a filename to open
        ifstream the_file ( argv[1] );
        // Always check to see if file opening succeeded
        if ( !the_file.is_open() )
          cout<<"Could not open file\n";
        else {
          char x;
          // the_file.get ( x ) returns false if the end of the file
          //  is reached or an error occurs
          while ( the_file.get ( x ) )
            cout<< x;
        }
        // the_file is closed implicitly here
      }
    }
    Where and how do you tell the program what argv[0] and argv[1] are?
    My computer is awesome.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    program-name arg1 arg2 arg3 arg4

    the OS is responsible for filling arg0 and it can be many different values.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    So you would put stuff like
    Code:
    int main ( int argc, char *argv[My Program,int x,int y] )
    My computer is awesome.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    nope, its done when you start the program. These are command line arguements

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    So its done automatically? If thats the case then how can I tell what the arguments are?
    My computer is awesome.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    When you start a program in the OS, you type the programs name, and then you have the option of adding arguments. A lot of console programs use this, for example, with a lot of compilers your can add arguments like -wall telling the compiler to give all warnings. So the arguments in your main function are put there by the OS. The first one is the number of arguments, and the second one is an array of pointers to those arguments.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Just got done with another tutorial. What are linked lists used for?
    My computer is awesome.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    They're data structures. I've only ever used them for storing large amounts of data when I didn't know how much data there was, as you can't declare open-ended arrays in C. There may be other uses I haven't thought of.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try reading the tutorial and then asking in a new thread.
    http://www.cprogramming.com/tutorial/lesson15.html

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I just read that tutorial thats why I asked about it.
    My computer is awesome.

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Linked lists are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary.
    And the new thread part does apply when you're changing the subject like that.

  12. #12
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by sean_mackrory
    When you start a program in the OS, you type the programs name, and then you have the option of adding arguments. A lot of console programs use this, for example, with a lot of compilers your can add arguments like -wall telling the compiler to give all warnings. So the arguments in your main function are put there by the OS. The first one is the number of arguments, and the second one is an array of pointers to those arguments.
    I've found you can pass the path and filename of a file to a program in Windows by dragging and dropping the file onto the executable. I've found that quite useful in the past.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM

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