Question
Regarding the below question I asked. I am having trouble running the program from the command line and including the filename with its extension as
Regarding the below question I asked. I am having trouble running the program from the command line and including the filename with its extension as the command line argument. The file is in the same directory how and where do I add the file extension?
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() function. 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 programs 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
#include
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 " \tUsage: " exeName " [filename] ";
cout " \tProgram returns wordcount of filename. ";
}
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 " \t**Error opening input file. ";
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 programs 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started