Command-line arguments, C/C++ Programming

Command-line arguments are passed into programs using the arguments of main(). Here's a quick example, for a program called by typing

"progname file.txt 1 2.7":
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
char *progname, *filename;
int value1;
float value2;
if (argc != 4) {
cerr << "usage: " << argv[0] << " file.txt int float" << endl;
exit(1);
}
progname = argv[0]; // argv[0] is the executable's name itself
filename = argv[1];
value1 = atoi(argv[2]); // convert C-string to int with atoi()
value2 = atof(argv[3]); // convert C-string to float with atof()
cout << progname << " " << filename << " " << value1 << " " << value2 <exit(0);
}

Posted Date: 3/12/2013 4:10:00 AM | Location : United States







Related Discussions:- Command-line arguments, Assignment Help, Ask Question on Command-line arguments, Get Answer, Expert's Help, Command-line arguments Discussions

Write discussion on Command-line arguments
Your posts are moderated
Related Questions
#include long BixFunction(int x, int y = 5, float z = 5) { return(++x * ++y + (int)++z); } int main() { cout return 0; }

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of t

I need to make a fractal trees in OpenGL C++ . The method used is to write a recursive function with cylinders of different sizes and angles to create the tree trunk and branches.

The voltage at the terminals of a battery is 52V when no load is linked and 48.8V when a load taking 80A is connected. Find the internal resistance of the battery. What wou


Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value: (x/y 2 +2y)/3 (a)

#question.gsdjfhjhjfkfk.

Need help with C network program

Draw a flowchart for a program that allows the user to enter 20 names along with their ages and their Social Security Numbers in three parallel arrays. Programs shou

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci