What is the arguments in main when we don't pass anything?

Discussion in 'C' started by Bazman, Jul 8, 2010.

  1. Bazman

    Bazman New Member

    Joined:
    Jul 8, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi there,

    I am now trialing the code below. It uses arguments supplied to the main function. I have no come across this before?
    When I run the code it just displays :

    usage: PR <filename>

    argc=1 and arv has a sting the tells you the file location.

    So where do the arguments come from?


    Code:
    
    #include <iostream>
    #include<fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    char ch;
    
    if(argc!=2){
    	cout << "usage: PR <filename>\n";
    	return 1;
    }
    
    ifstream in(argv[1], ios::in |ios::binary);
    if(!in){
    cout << "Cannot open file.\n";
    return 1;
    }
    
    while(in){
    	in.get(ch);
    	if(in) cout << ch;
    }
    
    	in.close();
    
    	return 0;
    }
    
    
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    3
    Trophy Points:
    0
    Normally the running executable is the first param to main and you are only getting that.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice