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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:44:11+00:00 2026-06-10T09:44:11+00:00

I have a big log file in which the records are sorted by time.

  • 0

I have a big log file in which the records are sorted by time. Each line has a time. I need to find all the records between time T1 and time T2 (T1 <= T2). I can scan the whole file line by line and find the start line with T1, copy that into a buffer and then scan the next line until I hit the end time T2. This will work but not very efficient.

I wonder if I can use binary search to locate the lines with time T1 and T2. But I am not sure how to determine the following:

  1. The middle line of the file
  2. How to determine the offset we should pass to lseek()?

Is that possible to use binary search on a file?

  • 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-10T09:44:12+00:00Added an answer on June 10, 2026 at 9:44 am

    Let us assume, that your lines are all reasonable near to the average length (meaning there is no line that will take up half of the log or so), which will make binary search feasible.

    Next I will also assume you will have following functions:

    //find the first start of a new log line after (or including) position start
    //return the last position of the file if no start could be found
    streampos findNextLineStart(ifstream &file, streampos start);
    //extract the data as a timestamp from a line
    int extractDate(ifstream &file, streampos lineStart);
    

    With these functions we can implement the following:

    //find the position of the first line whose date is bigger than the given
    streampos lower_bound(ifstream &file, int date)
    {
      file.seekg(0, ios::end);
      streampos begin = 0, 
                end = file.tellg();
      while(begin < end)
      {
         streampos cur = (begin + end) / 2;
         streampos start = findNextLineStart(file, cur);
         //was a line start found?
         if(start < end)
         {
            int lineDate = extractDate(file, start);
            if(lineDate < date)
              begin = start;
            else
              end = start;
         }
         else
           //narrow the bound as no line was found
           end = cur;
      }
      return begin;
    }
    

    I do not guarantee for this to work (on all corner cases) but it sketches the overall implementation. One would use another function upper_bound and with those you could get start and end of the lines that are within your bounds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a log file which has a format of this kind: DATE-TIME ###
I have a big log file, over 1 million lines. I need to use
I have a file test.log. Very big log file. It has different levels of
I have a JBoss 6 Server running which generates a big log file. Does
I have big problems with stopping a service, which plays audio file. I want
For some reason I have a file error.log which is 16GB In my Repositories
I have to parse a big bunch of log files, which are in the
I have a question for you: I have a big log file and I
I have an application generating a really heavy big log file every days (~800MB
I have big URL list, which I have to download in parallel and check

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.