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

This program is to be written in C .Complete and full answer in order to get cre

ID: 3739986 • Letter: T

Question

This program is to be written in C.Complete and full answer in order to get credit. Create a file date.dat with valid dates in the format month/day/year Ex. "7/2/1800", then write in C a program that takes input via the command-line argument a series of dates from the file date.dat , Ex. of input "./maindate < dates.dat ".The program will accept these validated dates in the month/day/year format and will convert each of them to the day, abbreviated month & year format . The abbreviated month should consist of the first three letters of the month, capitalized. These converted dates will be redirected to the output file (e.g. output.dat). Upon completion, this output will be followed by a copy of the complete original (dates.dat) . The program also must have a way to ‘know’ that the program is finished piping dates.Output: print validated dates.

Explanation / Answer


/**C program that read a file from the command line argv[1]
and read the dates and then write the abbreviated date
to output.txt file*/
//main.c
//include header files
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
//function prototype
char *getMonthName(int month);
int main (int argc, char*argv[])
{
   char *fileName;
   char date[50];
   char *p;
   int month;
   int day;
   int year;

   fileName=argv[1];

   FILE *fp;
   fp=fopen(fileName,"r");

   FILE *fout;
   fout=fopen("output.txt","w");

   if(!fp)
   {
       printf("File NOt Found ");
       getch();
       return -1;
   }

   //read file until end of file
   while(!feof(fp))
   {
       fscanf(fp, "%s",date) ;
       month = atoi(strtok (date,"/"));
       day = atoi(strtok (NULL,"/"));
       year=atoi(strtok (NULL,"/"));

       printf("%d/%d/%d",month,day,year);
       fprintf(fout,"%d/%d/%d",month,day,year);
       fprintf(fout," = %d %s,%d ",day,getMonthName(month),year);
       printf(" = %d %s,%d ",day,getMonthName(month),year);
   }


   fclose(fp);
   fclose(fout);

   getch();
   return 0;
}
/*The funciton that takes month and
returns the string of the month name
which contians the first three letters
of month*/
char *getMonthName(int month)
{
   char *mName;
   switch(month)
   {
   case 1:
       mName="Jan";
       break;
   case 2:
       mName="Feb";
       break;
   case 3:
       mName="Mar";
       break;
   case 4:
       mName="Apr";
       break;
   case 5:
       mName="May";
       break;
   case 6:
       mName="Jun";
       break;
   case 7:
       mName="Jul";
       break;
   case 8:
       mName="Aug";
       break;
   case 9:
       mName="Sep";
       break;
   case 10:
       mName="Oct";
       break;
   case 11:
       mName="Nov";
       break;
   case 12:
       mName="Dec";
       break;
   }

   return mName;
}

Sample Input:

dates.txt

7/2/1800
7/3/2018

Sample Output :

output.txt

7/2/1800 = 2 Jul,1800
7/3/2018 = 3 Jul,2018

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