Write a C++ program that loops through all of the command line arguments that ar
ID: 3886187 • 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
i. 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
ii. 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
Explanation / Answer
#include <stdio.h>
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
char ch;
char fname[50];
int ccount = 0;
printf(" Enter a FileName: ");
gets(fname);
fp = fopen(fname, "r");
if(!fp)
{
printf(" %s FILE NOT FOUND", fname);
}
else
{
while((ch=getc(fp) != EOF))
{
if(ch != ' ' && ch != ' ')
{
++ccount;
}
}
}
printf(" The filename %s has %d characters in the file",fname, ccount);
fclose(fp);
return 0;
}
OUTPUT
file1.txt
HelloWorld
Enter a FileName: file1.txt
The filename file1.txt has 10 characters in the file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.