Question

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 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);
}

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

CODE:

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

int main(int argc, char* argv[]) {
   FILE *fp;
   char ch;
   int num;
   long length;

   if(argc != 3) {
        printf("Please input correctly.");
        exit(0);
   }

   num = atoi(argv[2]);


   fp = fopen(argv[1], "r");
   if (fp == NULL) {
       puts("Cannot open this file");
       exit(0);
   }

   fseek(fp, 0, SEEK_END);
   length = ftell(fp);
   fseek(fp, (length - num), SEEK_SET);

   do {
       ch = fgetc(fp);
       putchar(ch);
   } while (ch != EOF);

   printf("\n");

   fclose(fp);
   return(0);
}

RUN AND OUTPUT:

hello.txt (actual file content)

PLEASE MAKE SURE TO LIKE THE ANSWER. THANKS SO MUCH IN ADVANCE :)

Add a comment
Know the answer?
Add Answer to:
The program is written in c. How to implement the following code without using printf basically...
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...

  • 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(),...

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

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • Design and implement a C version of the color match program. As a starting point, use...

    Design and implement a C version of the color match program. As a starting point, use the file HW2-1-shell.c. The program should employ a reasonable algorithm that compares all pairings of colors in the palette exactly once. A color should not be compared to itself. Nor should two colors be compared to each other more than once. Your C program should print out the total component color difference for the closest pair using the print string provided in the shell...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int...

    I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int main(int argc,char* argv[]) {      if(argc > 1)      {            char* commandName;            for(int i=2;i            {                   forkChild = fork();                   signal(SIGINT,sighandler);                   if(forkChild == 0)                   {                          execlp(commandName,commandName,argv[i],NULL);                          exit(0);                   }                   else                   {                          wait(NULL);                   }       } My problem is that I would like to kill the child with ^C but leave the parent running....

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

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