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 6881883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:11:07+00:00 2026-05-27T05:11:07+00:00

I have a large file and I need to get only the last line

  • 0

I have a large file and I need to get only the last line from it (\n only is the line separator).
I need this to be done on iOS device, so it cannot take much memory or cpu time (like reading the whole file).
How can I do this in either Objective-C,c++ or c++11?

  • 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-05-27T05:11:08+00:00Added an answer on May 27, 2026 at 5:11 am

    I have the feature in my production code.The idea is that try to read the last line by seeking and reading.Take a look please.

    bool readLastLine(std::string const& filename, std::string& lastLine)
    {
        std::ifstream in(filename.c_str(),std::ifstream::binary);
        if(!in) return false;
        in.seekg(0, std::ifstream::end);
        const std::streamoff len = in.tellg();
        //empty file
        if(len == 0)
        {
            lastLine = "";
            return true;
        }
        int buf_size = 128;
        std::vector<char> buf;
        while(in)
        {   
            if(buf_size > len)
            {
                buf_size = len;
            }
            buf.resize(buf_size);
            in.seekg(0 - buf_size, std::ifstream::end);
            in.read(&buf[0],buf_size);
            //all content is in the buffer or we already have the complete last line
            if(len == buf_size || std::count(buf.begin(), buf.end(), '\n') > 1)
            {
                break;
            }
            //try enlarge the buffer
            buf_size *= 2;
        }
        //find the second line seperator from the end if any
        auto i = std::find(++buf.rbegin(),buf.rend(), '\n');
        lastLine.assign(i == buf.rend() ?  buf.begin() : buf.begin() + std::distance(i, buf.rend()), buf.begin() + buf_size);
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large text file I am reading from and I need to
I have a large .txt file of records that I need to convert into
I have a a large text file (over 70mb) and need to count the
I have a very large data file with around 60000 rows. I need to
I have a large file where each line contains a substring such as ABC123.
I have a large file I want to download from a server I have
I have a large file, with 1.8 million rows of data, that I need
How can I read int from a file? I have a large(512MB) txt file,
I have a large file(60mb) and I am reading the file into a string
I have a large file that needs to be processed before feeding to another

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.