Question

Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested for file open.”

Append to the program to output to the text log file a new line starting with day time date followed by the message "SUCCESSFUL".

Append that message to a file “7Error_Log_File.txt” . ?newline

Remember to be using fprintf using stderr using return using exit statements. Test for file existence, test 7NoInputFileResponse.txt file not null (if null use alternate text string “99 error unknown ” from within the program), existence of 7Error_Log_File.txt otherwise display such message using fprintf stderr and exit.

This is what I have down so far, but I'm not sure how to put in day, time, date following "SUCCESSFUL"

#include <stdio.h>

#include <stdlib.h>

void filecopy(FILE *ifp, FILE *ofp)
{
   int c;
   while ((c = getc(ifp)) != EOF)
   putc(c, ofp);

}

   int main(int argc, char *argv[])

{

   FILE *fp;
   void filecopy(FILE *, FILE *);
   char *prog = argv[0];
   if (argc == 1)

{
   FILE *infile = fopen("7NoInputFileResponse.txt", "r");
   char error_message[100] = "99 error unknown";
   FILE *outfile = fopen("7Error_Log_File.txt", "a");
   if (infile == NULL)

{

   if (outfile != NULL)

{

   fprintf(outfile, error_message);
   fprintf(outfile, "\n");
   exit(0);

}

}
   if (outfile == NULL)

{

   fprintf(stderr, error_message);
   exit(0);

}

   filecopy(infile, outfile);
   fprintf(outfile, "\n");
   fclose(infile);
   fclose(outfile);

}

   else
   while (--argc > 0)
   if ((fp = fopen(*++argv, "r")) == NULL)

{

   fprintf(stderr, "%s: can not open %s\n", prog, *argv);
   exit(1);

}

else

{

   filecopy(fp, stdout);
   fclose(fp);

}

   if (ferror(stdout))

{

   fprintf(stderr, "%s: error writing stdout\n", prog);
   exit(2);

}

   exit(0);
}

Please screenshot results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Main.c

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include<time.h>

void filecopy(FILE *ifp, FILE *ofp)
{
   int c;
   while ((c = getc(ifp)) != EOF)
       putc(c, ofp);
}

char* getDay(int d)
{
   switch (d)
   {
   case 0: return "Sunday ";
           break;
   case 1: return "Monday ";
           break;
   case 2: return "Tuesday ";
           break;
   case 3: return "Wednesday ";
           break;
   case 4: return "Thursday ";
           break;
   case 5: return "Friday ";
           break;
   case 6: return "Saturday ";
           break;
   default:
       break;
   }
}

int main(int argc, char *argv[])
{
   FILE *fp;
   void filecopy(FILE *, FILE *);
   char *prog = argv[0];
   if (argc == 1)
   {
       FILE *infile = fopen("C:\\hanu\\7NoInputFileResponse.txt", "r");
       char error_message[100] = "99 error unknown";
       FILE *outfile = fopen("C:\\hanu\\7Error_Log_File.txt", "a");
       if (infile == NULL)
       {
           if (outfile != NULL)
           {
               fprintf(outfile, error_message);
               fprintf(outfile, "\n");
               exit(0);
           }
       }
       if (outfile == NULL)
       {
           fprintf(stderr, error_message);
           exit(0);
       }

       filecopy(infile, outfile);
       fprintf(outfile, "\n");
       time_t t = time(NULL);
       struct tm tm = *localtime(&t);
      
       fprintf(outfile, getDay(tm.tm_wday));
       fprintf(outfile, "%d-%d-%d %d:%d:%d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
       fprintf(outfile, " SUCCESSFUL");
      
       fclose(infile);
       fclose(outfile);
   }

   else
       while (--argc > 0)
           if ((fp = fopen(*++argv, "r")) == NULL)
           {
               fprintf(stderr, "%s: can not open %s\n", prog, *argv);
               exit(1);
           }
           else
           {
               filecopy(fp, stdout);
               fclose(fp);
           }

   if (ferror(stdout))
   {
       fprintf(stderr, "%s: error writing stdout\n", prog);
       exit(2);
   }
  
   exit(0);
  
}

//output

Add a comment
Know the answer?
Add Answer to:
Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

  • Below is a basic implementation of the Linux command "cat". This command is used to print...

    Below is a basic implementation of the Linux command "cat". This command is used to print the contents of a file on the console/terminal window. #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) {FILE *fp; if(2 != argc) {priritf ("Usage: cat <filename>\n"); exit(1);} if ((fp = fopen(argv[1], "r")) == NULL) {fprintf (stderr, "Can't. open input file %s\n", argv[1]); exit (1);} char buffer[256]; while (fgets(X, 256, fp) != NULL) fprintf(Y, "%s", buffer); fclose(Z); return 0;} Which one of the following...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage:...

    #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage: * ./wc <words | lines> <file> * * If argv[1] is "words", then you should count the number of words. If it * is "lines", then you should count the number of lines. * * For example: * $ cat a.txt * a b c d * $ ./wc words a.txt * 4 * $ ./wc lines a.txt * 1 * * YOUR PROGRAM...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT