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

  • SEARCH
  • Home
  • 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 8978369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:35:42+00:00 2026-06-15T19:35:42+00:00

Using C, is there a way to read only the last line of a

  • 0

Using C, is there a way to read only the last line of a file without looping it’s entire content?

Thing is that file contains millions of lines, each of them holding an integer (long long int). The file itself can be quite large, I presume even up to 1000mb. I know for sure that the last line won’t be longer than 55 digits, but could be 2 only digits as well. It’s out of options to use any kind of database… I’ve considered it already.

Maybe its a silly question, but coming from PHP background I find it hard to answer. I looked everywhere but found nothing clean.

Currently I’m using:

if ((fd = fopen(filename, "r")) != NULL) // open file
{
    fseek(fd, 0, SEEK_SET); // make sure start from 0
    while(!feof(fd))
    {
        memset(buff, 0x00, buff_len); // clean buffer
        fscanf(fd, "%[^\n]\n", buff); // read file *prefer using fscanf
    }
    printf("Last Line :: %d\n", atoi(buff)); // for testing I'm using small integers
}

This way I’m looping file’s content and as soon as file gets bigger than ~500k lines things slow down pretty bad….

Thank you in advance.
maxim

  • 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-15T19:35:44+00:00Added an answer on June 15, 2026 at 7:35 pm

    If there is a maximum line length, seek to that distance before the end.
    Read up to the end, and find the last end-of-line in your buffer.

    If there is no maximum line length, guess a reasonable value, read that much at the end, and if there is no end-of-line, double your guess and try again.

    In your case:

    /* max length including newline */
    static const long max_len = 55 + 1;
    /* space for all of that plus a nul terminator */
    char buf[max_len + 1];
    
    /* now read that many bytes from the end of the file */
    fseek(fd, -max_len, SEEK_END);
    ssize_t len = read(fd, buf, max_len);
    
    /* don't forget the nul terminator */
    buf[len] = '\0';
    
    /* and find the last newline character (there must be one, right?) */
    char *last_newline = strrchr(buf, '\n');
    char *last_line = last_newline+1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there way that I can read the file from remote server using fopen
Is there any way to read a file using sharpsvn................
Is there any way to read a TIFF file using AIR ? Looks like
In C#/.NET (on Windows) is there a way to read a growing file using
Is there a simple way, using C#, to open an arbitrary URL, read in
Using htmlentities() is there a way I can set to allow only <b> and
I am using FileHelpers to read a CSV file that have hundreds of columns
From the native Win32 API using C++ is there a way to determine whether
Is there a way using the Net::SFTP Library in Ruby ( API Link )
Is there a way using Java to over-ride the browser authentication dialog box when

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.