C++ Command Line Arguments

C++ Command Line Arguments

Like C, C++ language also supports a feature that facilitates the supply of arguments to the main() function. These arguments are supplied at the time of invoking(call) the program.They are typically used to pass the names of data files

Example

c >>exam data results

here exam is the name of the file containing the program to be executed. and data and results are the filenames passsed to the program as command line arguments

The command line arguments are typed by the programmer(user) and delimited by a space. The first argument will always the filename(command name) and contains of the program to be executed how do these arguments get into the program ?

in c++ main() function which we have been using up to now without any arguments can take two arguments as shown in the below

main(int argc, char * argv[])

The first arguments argc(know as argument counter) represent the number of arguments in the command line.The second argument argv(known as argument vector) is an array of char type pointers that points to the command line arguments. The size of the array will be equal to the value of argc(argument) for instance for the command line

c > exam data results

The value of argc would be 3 and the argv would be an array of three pointers to string as shown below

argu[0]–>exam
argu[1]–>data
argu[2]–>results

Note that argv[0] always represent the command name that invokes the program. The character pointers argv[1] and argv[2] can be used as file name in the file opening

statement as shown in below
……
……
infile.open(argv[1]); // open data file for reading
…..
…..
outfile.open(argv[2]); // open result file for writing
…..
…..

in the below program the use of the command line arguments for supplying the file names. The command line is

test ODD EVEN

The program creates two files called ODD and EVEN using the command line arguments, and a set of number stored in an array are written to these files. Note that the odd number =s are written to the file ODD and the even numbers are written to the file EVEN. The program then displays the contents of the files

#include < iostream>
#include < fstream>
#include < stdlib.h>
int main(int argc,char * argv[])
{
int number[9]={11,22,33,44,55,66,77,88,99};
if(argc!=3)
{
std::cout << "argc=" << argc << "\n";
std::cout << "Error in arguments";
exit(1);
}
std::ofstream fout1,fout2;
fout1.open(argv[1]);
if(fout1.fail())
{
std::cout <<"could not open the file";
exit(1);
}
fout2.open(argv[2]);
if(fout2.fail())
{
std::cout << "could not open the file" << argv[2] << "\n";
exit(1);
}
for(int i=0;i < 9;i++)
{
if(number[i]%2==0)
fout2 << number[i] << " ";
else
fout1 << number[i] << " ";
}
fout1.close();
fout2.close();
std::ifstream fin;
char ch;
int i;
for(i=1;i < argc;i++)
{
fin.open(argv[i]);
std::cout << "contest of" << argv[i] << "\n";
do
{
fin.get(ch); // read a value
std::cout << ch;
}
while(fin);
std::cout << "\n\n";
fin.close();
}
return 0;
}

output:
contents of ODD
11,33,55,77,99

Contents of EVEN
22 44 66 88

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise