Question

Write a C program called last10 that prints the last ten lines of a text file....

Write a C program called last10 that prints the last ten lines of a text file. The program can be used from the command line with:

last10 filename or

last10

If there is no filename, last10 processes standard input.

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

Here is the solution for above problem

1) In the solution below I am trying to first calculate the total number of lines in a file, after that I am reading the same file again till the last ten line. At the last ten lines I will use the previously calculated number of Lines to print the last ten lines . I have provided the necessary comments in the program below to understand it

C Code

#include<stdio.h>
#include<stdlib.h>
int main(int argc , char **argv)
{

    char *filename=(char *) malloc(sizeof(char) * 1000);
    // reading the input argument filename
    if(argc>1) {
        filename = argv[1];
    }
    else {
        //if input argument is not present taking the file name input from the console
        printf("Enter the file Name: ");
        scanf("%s", filename);
    }
    //for storing a single line from the file
    char *line= (char *) malloc(sizeof(char)*1000);
    int noOfLines=0;//calculating the number of lines in the file
    size_t length=0;// size of each line
    ssize_t read;
    FILE *ptr; // file pointer
    int count=0;
    //trying to open a file
    if((ptr= fopen(filename,"r"))==NULL)
    {
        printf("Cannot open the file");
    }
    //reading the file line by line and calculating the number of lines in the line
    while((read=getline(&line,&length,ptr))!=-1)
    {
        noOfLines++;
    }
// closing the file
    fclose(ptr);
    //trying to open the file
    if((ptr= fopen(filename,"r"))==NULL)
    {
        printf("Cannot open the file");
    }
    //reading the file line by line
    while((read=getline(&line,&length,ptr))!=-1)
    {
        if(count>=(noOfLines-10)) // this count variable is used to check if we are at the last 10 lines
            printf("%s\n",line);
        count++;
    }
   //closing the file again
    fclose(ptr);

    return 0;
}

INPUT FILE (It is from the sherlock holmes novel)

OUTPUT OF CODE

Screenshot of code

Add a comment
Know the answer?
Add Answer to:
Write a C program called last10 that prints the last ten lines of a text file....
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
  • Write a c program that takes in a text file and writes lines with only lowercase...

    Write a c program that takes in a text file and writes lines with only lowercase letters to another text file which is disclosed from the command line. If no text file to output to is disclosed in the command line, the lines with only lowercase letters should be written into a text file called "all_lower.txt"

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

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

  • Write a program file_max.c that reads a set of integer numbers from a file and prints...

    Write a program file_max.c that reads a set of integer numbers from a file and prints out the maximum number to standard output. The name of the input file should be specified as a command line argument.

  • Sum Second Write a shell (text-based) program, called sum_second.py, that opens a text file calle...

    Sum Second Write a shell (text-based) program, called sum_second.py, that opens a text file called stuff.txt with 2 numbers per line separated by commas and prints out the sum of the second numbers on every line. For example, if stuff.txt has the following lines: 500,55 300,45 200,7 400,20 Then the following example output would happen. PS C:\Users\ssiva\Desktop> python sum_second.py Sum: 127 PS C:\Users\ssiva\Desktop>

  • You are required to write a C program on Unix/Linux in which the parent process creates...

    You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Write a program for IJVM called scramble that takes lowercase text from input and prints the...

    Write a program for IJVM called scramble that takes lowercase text from input and prints the next character (i.e. ‘b’ is printed as ‘c’) and uppercase text and prints the character before (‘B’ is printed as ‘A’). Print all other characters as is.

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