Conversions    
 
  Constantes 
 
  Kepler's laws  
 
  Unités  
 
  ask us  
 


Coding &
programming


Installing CodeBlocs compiler

C language


C++ language




© The scientific sentence. 1998-2016 
 

Programming with C/C++ Language
Command line arguments in C++





1. line arguments


As in C, C++ accepts command line arguments.

Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux. They are passed in to the program from the operating system.

To use command line arguments in C++ program, we need to understand the full declaration of the main function, which previously has accepted no arguments.

In fact, main can actually accept two arguments: argc and argv.

The first argument argc is number of command line arguments, and the other argument argv is a full list of all of the command line arguments.

Therefore, the full declaration of main looks like this:

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

The integer, argc is the ARGument Count (hence argc). The argument argc is the number of arguments passed into the program from the command line, including the name of the program.

The array of character pointers is the listing of all the arguments.

argv[0] is the name of the program, or an empty string if the name is not available.

We can use each argv element just like a string, or use argv as a two dimensional array.

Note that argv[argc] is a null pointer.

These two argments are generally used to write a function that takes the name of a file and outputs the entire text of it onto the screen.



2. Example


 
#include 
#include 

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] <<" \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
  }
}

  


Using CodeBlocs:





3. Inline functions


Inline functions are used to save time at a cost in space. Once we define an inline function, using the inline keyword, whenever we call that function, the compiler will replace the function call with its actual code from the function.

Example

#include
using namespace std;
inline void lemon()
{
cout<<"citron is a fruit";
}
int main()
{
citron(); // Call it like a normal function ...
cin.get();
}

However, once the program is compiled, the call to citron(); will be replaced by the code making up the function.
Inline functions are best for small functions that are called often.








  

Google
  Web ScientificSentence
 


chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2009. All rights reserved.