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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%
  • 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.

 

A CAWINDOWS\system32\cmd.exe
C:\Users \Adam \Desktop\CYBR210 Code>count
Usage: count [-option] [filename]
Opt ions:
lettercount
wordcount
C:\Users \Adam\Desktop\CYBR210 Code>count -1 hi.txt
Lettercount:8
C: \Users\Adam\Desktop\CYBR210 Code>count -w hi.txt
Wordcount : 3
C:\Users\Adam\Desktop\CYBR210 Code>_
Transcribed Image Text:A CAWINDOWS\system32\cmd.exe C:\Users \Adam \Desktop\CYBR210 Code>count Usage: count [-option] [filename] Opt ions: lettercount wordcount C:\Users \Adam\Desktop\CYBR210 Code>count -1 hi.txt Lettercount:8 C: \Users\Adam\Desktop\CYBR210 Code>count -w hi.txt Wordcount : 3 C:\Users\Adam\Desktop\CYBR210 Code>_
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education