Question

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 command line to be tested for file open.”

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.

#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

//do comment if any problem arises

//modified code has been highlighted

#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 */

//open input file

FILE *infile = fopen("7NoInputFileResponse.txt", "r");

//error message

char error_message[100] = "99 error unknown";

//open output file

FILE *outfile = fopen("7Error_Log_File.txt", "a");

//if file oppened successfuly then read into error message

if (infile == NULL)

{

//if outfile is opened successfully

if (outfile != NULL)

{

fprintf(outfile, error_message);

fprintf(outfile, "\n");

exit(0);

}

}

//if outputfile doesn't open successfuly

if (outfile == NULL)

{

//print error message to stderr

fprintf(stderr, error_message);

//exit

exit(0);

}

//copy contents of input file to output file

filecopy(infile, outfile);

//append newline

fprintf(outfile, "\n");

fclose(infile);

fclose(outfile);

}

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 */

}

Output:

ran this program 2 times first when input file is available and second when input file is deleted

these are contents of output file:

Add a comment
Know the answer?
Add Answer to:
C Language Programming. Using the program below - When executing on the command line only this...
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
  • 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...

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

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

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

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

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

  • Write a C language program that will prompt for 12 different resistor values, entered one at...

    Write a C language program that will prompt for 12 different resistor values, entered one at a time via the keyboard, expressed in Ohms. Valid values are 0.01 Ohm up to 10,000,000,000 Ohms. Write each entry to a text file on the disk thus: Resistor 1 Ohmic value = 2200. Update the resistor number for each of the 12 entries. Do not repeat any resistance value. Write a second C language program to read the 12 entries from the text...

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

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate 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
ADVERTISEMENT