Problem 5 (8 points): complete the code Complete the C code snippet below to ope
ID: 3753392 • Letter: P
Question
Problem 5 (8 points): complete the code Complete the C code snippet below to open a file passed to a program as «argc-1> from the command line for reading, program character in the file and adds up the total number of cargc-2> characters read to count. Read characters until the Eor is reached. Fill in the code where blank so the code executes and works correctly. Example: ./a.out ThisSourceCode.c a >> returns: Num of e's: tincludecstdio.h> tinclude int main int argc, char arg) itt fprintf (stderr, "zong nunber of arguments td in", arge) return EXIT FAILORE: 1 FILL IN HERE 1 FILE *in- Eopen t Lf (in NULL) tprintf(atderr, "Could not open fileln") return EXITFALURE; - /* read character, add one to count if matches the letter g int count 0 char c. c2: e2argv [21 101a /e complete code below here FILL IN HERE 1 //// TLLIN HERE //// while / end code/ printt "Num of sc's: dn",c2, count) return ExXIT SUCCESSExplanation / Answer
#include <stdio.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
if( argc != 3 )
{
fprintf(stderr , "Wrong number of arguments %d ", argc);
// exit the program
return EXIT_FAILURE;
}
char str[50];
// open file in read mode
FILE *in = fopen(argv[1], "r");
// if unable to open file
if( in == NULL )
{
fprintf(stderr , "Could not open file ");
// exit the program
return EXIT_FAILURE;
}
int count = 0;
// get the character passed as second argument
char c2 = argv[2][0];
// read a character in a file
char c = fgetc(in);
// loop untill file ends
while( c != EOF )
{
//printf("%c", ch);
// if urrent charactrer matches the required character
if( c2 == c )
count++;
// read a character in a file
c = fgetc(in);
}
printf("Number of %c's : %d '", c2 , count);
// close the file
fclose(in);
return 0;
}