Question

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 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". See example 7-8MiscFunctionsTime on Blackboard.

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

Remember to be using fprintf; stderr; return; and 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.



#include <stdio.h>
#include <stdlib.h> /*needed for stderr and exit */
/* cat: concatenate files, version 2 */

/* filecopy: copy file ifp to file ofp */
void filecopy(FILE *ifp, FILE *ofp) /* pointer to input file, output file */
{
   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];  /* this program exe name as source of an error */
   
   if (argc == 1){ /* no args; copy standard input */
      filecopy(stdin, stdout);  /* keyboard_input is repeated as display_output, ctrl_break to exit */
   }
   else
      while (--argc > 0)
         if (( fp = fopen(*++argv, "r")) == NULL) { /* pointer to next command line file */
            fprintf(stderr, "%s: can not open %s\n", prog, *argv);
            exit(1);  /* determined by programmer return value for error type #1 */
         } else {
            filecopy(fp, stdout);
            fclose (fp);
         }
         
   if(ferror(stdout)) {
      fprintf(stderr, "%s: error writing stdout\n", prog);
      exit(2);  /* determined by programmer return value for error type #2 */
   }
   
   exit(0);  /* determined by programmer return value for error type #0, success */
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

//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("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");
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:-

7Error_Log_File.txt - Notepad File Edit Format View Help There were no arguments on the command line to be tested for file op

Add a comment
Know the answer?
Add Answer to:
Modify When executing on the command line having only this program name, the program will accept...
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...

  • 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...

  • 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...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Consider the following, partially-complete, C-program: #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #define BUFFERSIZE 100 #define COPYMODE...

    Consider the following, partially-complete, C-program: #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #define BUFFERSIZE 100 #define COPYMODE 0644 main(int argC, char* argv[]) {    int srcFd;    int dstFd;    int charCnt;    char buf[BUFFERSIZE];    /*Check args*/    if( argC!=3 ){        fprintf( stderr, "usage: %s source destination\n", argv[0]);        exit(1);    }    /*Open the files*/       srcFd= open(argv[1],O_RDONLY);    if( srcFd==-1 ){        fprintf(stderr,"Cannot open %s \n", argv[1]);    }    dstFd= creat(argv[2],COPYMODE);   ...

  • Need this in c programming

    Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...

  • 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...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

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