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

C Language Programming. Using the program below - When executing on the command line only this...
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...
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 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 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...
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 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 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...
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 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 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...