Question

Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

Command line input

In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line arguments, and the other argument is a full list of all of the command line arguments in an array of strings.

The full header of main looks like this:

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

The integer, argc is the argument Count (hence argc). It 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. After that, every element number less than argc is a command line argument. It can use each argv element just like a cstring or use argv as a twodimensional array.

How could this be used? Almost any program that wants its parameters to be set when it is executed would use this.

For example:

#include <iostream>

using namespace std;

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

// process command line arguments

cout << "The name of the program is " << argv[0] << endl;

if (argc == 1) {

cout << "Usage: Provide some command line arguments!\n";

} else {

cout << "The command line arguments are:\n";

for (int i = 1; i < argc; ++i) {

int value = std::atoi(argv[i]);

cout << argv[i];

if (value != 0) {

cout << " (value: " << value << ")\n";

}

cout << endl;

}

}

return 0;

}

This demonstration program is fairly simple. It first checks that the user added a second argument. The program checks to see if the argument is valid by trying to convert it using std::atoi.

The program gets the name of the file from the command line only. Do not interactively prompt the user for the file name.

• If no command line arguments are passed to the program, the program should display a usage message to the user and terminate.

• If more than 2 arguments are passed to the program, the program should display a usage message to the user and terminate.

A correct use would be:

./a.out file.txt

An incorrect use would be:

./a.out

Error: Specify the file name as a command line argument.

For example: ./a.out file1.txt

./a.out f1.txt f2.txt

Error: Specify the file name as a command line argument.

For example: ./a.out file1.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::::

#include <iostream>

using namespace std;

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

   // process command line arguments
   cout << "The name of the program is " << argv[0] << endl;
   if (argc <3) {
       cout << "Error: Specify the file name as a command line argument.";
       cout<<"\nFor example: ./a.out file1.txt file2.txt";
   }else if (argc > 3) {
       cout << "Error: Too many argument passed!!!,Specify the file name as a command line argument.";
       cout<<"\nFor example: ./a.out file1.txt file2.txt";
   }else {
       cout<<"File One name is : "<<argv[1]<<endl;
       cout<<"File Two name is : "<<argv[2]<<endl;

   }
   return 0;

}

command  

Add a comment
Know the answer?
Add Answer to:
Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • In C Programming Adding to your program in part A, go through the command line arguments...

    In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A:             Three two one And then it would display   The smallest string was:...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the...

    I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the maximum-subarray problem. Start at the left end of the array, and progress toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum subarray of A[1..j], extend the answer to find a maximum subarray ending at index j + 1 by using the following observation: a maximum subarray of A[1..j + 1] is either a maximum subarray of A[1..j] or...

  • 1. In ANSII standard C++, there is no library function to convert an integer to a...

    1. In ANSII standard C++, there is no library function to convert an integer to a string. Your program should develop such a function. In other words complete the following program using your itos function. (Use of other C functions is prohibitted) // this program will read in an integer and convert it to a string #include <iostream> #include <cstdlib> #include <string> using namespace std; string itos(int n) { } int main(int argc, char* argv[]){ int n = atoi(argv[1]); cout...

  • What is the output of the following code given the program was called using the command-line:...

    What is the output of the following code given the program was called using the command-line: a.out input.txt output.txt int main(int argc, char* argv[]) { cout << argc; return 0; } Select one: a. 1 b. 2 c. 3 d. 6 which asnwer it is

  • Program already solved, but I need to put function documentation headers for each and every function...

    Program already solved, but I need to put function documentation headers for each and every function in your program (for the ones you write, and keep the function header I give you for the main() function). Also make sure you keep the file block header at the top of the file, and fill in the information correctly. Secondly this week you must get your indentation correct. All indentation must use 2 spaces, and you should not have embedded tabs in...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • Write a C++ program In this assignment you will complete the definition of two functions that...

    Write a C++ program In this assignment you will complete the definition of two functions that implement the repeated squaring algorithm described in the Stamp textbook on pages 98-99. Note that your implementation should not use the pow() function, the powl() functions, or any other built-in exponentiation functions. program4-driver.cpp - This file contains a completed main() function that serves as the driver to parse the command line, pass the values to the powerModN() function, and print the result. Make no...

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT