Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that loops through all of the command line arguments that ar

ID: 3883378 • Letter: W

Question

Write a C++ program that loops through all of the command line arguments that are passed to the program. For each argument, you are to test to see if there is a file with that name. [you test by attempting to open the file and checking to see if the open was successful]. Your program should do the following

1) If there is NOT a file with the name given in the argument, print, on a single line, the name of the file, a space, and the string FILE NOT FOUND

2) If there is a file with the name given in the argument, print, on a single line, the name of the file, a space, and an integer representing the number of characters in the file.

You will note that there should be one line of output for each command line argument that is specified.

Explanation / Answer

Hi,

C++ Code-

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
int c=0;
string line;
if (argc > 1) {
std::cout << argv[1] << std::endl;
ifstream ifile(argv[1]);
if (ifile) {
while( getline ( ifile, line ) )
{
cout << line << endl;
c += line.length();
}
cout<<argv[1]<<" " <<c;
}
else
{
cout<<argv[1]<<" "<<"FILE NOT FOUND";
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote