Question
#include // I/O library int main() { int t = 0, nc = 0, ns = 0, nd = 0;//t-total characters, nc-number of capital, ns-number
#include // I/O library
int main() { int t = 0, nc = 0, ns = 0, nd = 0;//t-total characters, nc-number of capital, ns-number of small, nd-number of digits FILE* fp; // file pointer char ch; // store characters read from file float pc, ps, pd;//pc-percentage of capital, percentage of small, percentage of digits fopen_s(&fp, "characters.txt", "r"); // open file for reading if (fp == NULL) { // if open failed printf("Error: can't find data file. Abort program now. "); return 1; // terminate now } // read from file
while ((ch = fgetc(fp)) != EOF) { // read one char, go into the loop if not EOF // 1. process this char t++;//incrementing total number of characters if (ch >= 'A' && ch <= 'Z')//checking for capital letters { nc++;//incrementing number of capital } else if (ch >= 'a' && ch <= 'z')//checking for small letters { ns++;//incrementing number of small } else if (ch >= '0' && ch <= '9')//checking for digits {
nd++;//incrementing number of digits } } // end of reading file while loop //calculating percentage pc = (float)nc / t; ps = (float)ns / t; pd = (float)nd / t; printf("There are %d characters with %.2f%% upper case, %.2f%% lower case, %.2f%% digits", t, pc, ps, pd); fclose(fp); // close file return 0; } // end main
1. Modify your program to have it take the input file name as a command line argument. This way you can supply the input file name as the command line argument instead of modifying your code for each different testing file.
After you make this change your program should be executed as:
./hw3 myinput.txt
assuming hw3 is the name of your executable and myinput.txt is the input file.
Have your program print an error message and abort if it is invoked with incorrect number of command line arguments (too many or too few). For example if the program is invoked with one single argument (./hw3) (argc is 1, only the name of the executable), your program will display a message similar to this one:
Error: this program should be invoked with two command line arguments. For example: ./hw3 test.txt.
2. Run your program on Linux System with no file names and again with two input file names. Take a screenshot of each execution.
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