The program is written in c. How to implement the following code without using printf basically without
Question:
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 terminal) only the last 200 characters from that file. If the fille contains fewer than 200 characters, output the entire file contents. (Where the program completes normally, no other output should be produced.)
Your program should always exit `cleanly,' i.e. close any open fles (and free any resources you allocate) before termination. If there is a problem accessing the le (e.g. le does not exist), your program should display an error message (you should output this to the program's standard error stream rather than standard output file why?) and exit cleanly with a return value of 1. On successful completion, your program should return an error code of 0. Write up an instruction manual (user documentation) in a plain text file, explaining how to compile your program and how to use it. You may test your program using either your user documentation itself, or by using system log data found in many files under the /var/log/ directory.
4.Instead of the default 200 characters, allow the user to specify an -n argument at the command line in order to specify a diferent number of characters. Where the -n option is used, the argument immediately following it is treated as the number of characters to be used. If the next argument after an -n argument is not a non-negative integer, the program arguments are invalid (see below).
5. Allow the user to specify a diferent filename (instead of logfile.txt) by putting the filename in a command-line argument.
#include
int main(int argc,char *argv[]) {
FILE *fp;
char ch;
int num;
long length;
if ( argc ! = 3)
{
printf("please provide required arguments including number of chars and filename");
exit(1);
}
num=argv[1];
fp = fopen(*argv[2], "r");
if (fp == NULL) {
puts("cannot open this file");
exit(1);
}
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, (length - num), SEEK_SET);
do {
ch = fgetc(fp);
putchar(ch);
} while (ch != EOF);
fclose(fp);
return(0);
}