The next two problems introduce command-line arguments. Command-line arguments are commonly used in console-based applications. They afford the operator the ability to execute a program by passing in arguments from the command line. We need add two arguments to our main() The first argument is an int that contains the number of command line arguments, the second argument is an array of char pointers, which contain the actual command line arguments. The command line argument values are stored in an array, and can be accessed to perform different tasks. The first command line argument, argv[0] is always the name of the executable file. The remaining arguments can be accessed by indexing into the array. For example, the next argument would be located in argv[1]. When writing programs with command line arguments, it is important to clarify the program’s usage. This way the operator knows how to execute the program with the correct arguments. Here is an example program where the user enters a filename as a command line argument and the program performs a word count on the file.
- The next two problems introduce command-line arguments. Command-line arguments are commonly used in console-based applications. They afford the operator the ability to execute a
program by passing in arguments from the command line. We need add two arguments to our main() The first argument is an int that contains the number of command line arguments, the second argument is an array of char pointers, which contain the actual command line arguments. The command line argument values are stored in an array, and can be accessed to perform different tasks. The first command line argument, argv[0] is always the name of the executable file. The remaining arguments can be accessed by indexing into the array. For example, the next argument would be located in argv[1]. When writing programs with command line arguments, it is important to clarify the program’s usage. This way the operator knows how to execute the program with the correct arguments. Here is an example program where the user enters a filename as a command line argument and the program performs a word count on the file.
#include <iostream>
#include <fstream>
using namespace std;
//Pre: inFile is open
//Post: Returns number of words in file
int wordcount(ifstream& file)
{
int count = 0;
string word;
while(file >> word)
{ count++; }
return count;
}
//Pre: None
//Post: Prints Program Usage
void printUsage(char exeName[])
{
cout << "\n\tUsage: " << exeName << " [filename]\n";
cout << "\n\tProgram returns wordcount of filename.\n\n";
}
int main(int argc, char* argv[])
{
// Make sure user enters proper number of arguments
if (argc != 2)
{
printUsage(argv[0]);
return 1;
}
// File is a command line argument
ifstream inFile;
inFile.open(argv[1]);
// Check to see if file exists
if(!inFile)
{
cout << "\n\t**Error opening input file.\n\n";
return 1;
}
int wc = wordcount(inFile);
cout << wc << endl;
inFile.close();
return 0;
}
Your task is to write a program called “count” that lets the user retrieve a word count or a letter count of a file that is passed as a command line argument. The user enters a flag (-l or -w) to indicate if they want a letter count (-l) or word count (-w). See the figure below for an example of the program’s output. You must use a switch statement to handle the options. You should also have at least three functions: one to print the usage, one to handle the word count, and one to handle the letter count. The program must be executed from a command line environment. Show full source code and a screenshot of your working program.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images