Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:37:07+00:00 2026-06-11T14:37:07+00:00

I just got a semi function head program running and I need help making

  • 0

I just got a semi function head program running and I need help making it into a tail program where it will display x number of lines at the end of the file. Here is my head code.
I’m having trouble thinking of how to get the counter to read the entire file then start at the bottom and show the last lines of the file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1024


int numLines = 0;
int linecount = 0;
FILE *src = NULL;
char ch[MAX];


void GetArgs (int argc, char **argv){
if(argc != 4 || argc != 2) {
    printf("Error not enough arguments to continue \n", argv[0]);
    exit(-1);

}// end if argc doenst = 4 or 2

if(argc == 2){
    src = fopen( argv[1], "r:");
    numLines=10;

}// end argc = 2

if(argc == 4){
    if (strcmp (argv[1], "-n") !=0 ){
        numLines = atoi (argv[2]);
        src = fopen (argv[3], "r");
        if ( src == NULL){
            fputs ( "Can't open input file." , stdout);
            exit (-1);
        }
        while (NULL != fgets(ch, MAX, src)){
            linecount++;
            fputs(ch, stdout);
            if (linecount == numLines){
                break;
            }
        }

    }//end of nested if
     else if (strcmp (argv[2], "-n") !=0 ){
        numLines = atoi (argv[3]);
        src = fopen (argv[1], "r");
        if ( src == NULL){
            fputs ( "Can't open input file." , stdout);
            exit (-1);
        }
        while (NULL != fgets(ch,MAX, src)){
            linecount++;
            fputs(ch, stdout);
            if (linecount == numLines){
                break;
            }
        }            


    }//end of else
}//end if argc == 4

}// end GetArgs


int main(int argc, char **argv){



GetArgs(argc, argv);

   fclose( src );
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:37:08+00:00Added an answer on June 11, 2026 at 2:37 pm

    I used your variables and style. If I understand, you want to printout last [X] lines from file. [X] – parameter from CLI

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 1024
    
        FILE *src = NULL;
        FILE *src_end = NULL;
        int linecount = 0;
        char ch[MAX];
    
        int main(int arc, char **argv) {
            int i = 0;
            int letter = 0;
    
            if (arc != 4) {
                puts("Wrong parameters");
                return 1;
            }
    
            do {
                src = fopen(argv[1], "r");
                if (src == NULL) {
                    puts("Can't open input file.");
                    break;
                }
    
                if (0 != strcmp(argv[2], "-n")) {
                    puts("Wrong 2nd parametr");
                    break;
                }
    
                sscanf(argv[3], "%u", &linecount);
                printf("Printout last %u lines, from file:%s\n", linecount,
                        argv[1]);
    
                src = fopen(argv[1], "r");
                if (src == NULL) {
                    fputs("Can't open input file.", stdout);
                    return 1;
                }
    
                for (i = 2; linecount; i++) {
                    fseek(src, -i, SEEK_END);
                    letter = fgetc(src);
                    if (letter == 0x0a) { // 0x0a == "\n"
                        fgets(ch, MAX, src);
                        fputs(ch, stdout);
                        linecount--;
                    }
                }
            } while (0);
    
            fclose(src);
            return 1;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just got query regarding c malloc() function. I am read()ing x number of bytes
Just got a little question when a column got the Datatype Number(6,3) will this
just got a couple of new PanelPC's running Windows Embedded 2009 that we are
Just got the new iPad, and when I touch the microphone and say a
i just got a call from Hosting company, they claimed that my application creates
I just got an email from twitter saying my keys have now been enabled
I just got pagination to work on my site, but I wanted to use
I just got a job offer to be a FE Developer, but I've never
I just got out of a small project and I've tried to follow a
We just got our first major Perl CGI assignment in my CS class. Our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.