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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:08:51+00:00 2026-05-25T20:08:51+00:00

I have a binary search on a file. The file is filled with log

  • 0

I have a binary search on a file. The file is filled with log messages where each line begins with a date (dates or sorted based on event occurs)

example:

  • 2011-09-18 09.38.20.123
  • 2011-09-18 09.38.20.245
  • 2011-09-18 09.38.20.393
  • 2011-09-18 09.38.20.400
  • 2011-09-18 09.38.20.785

If i need to find this date for example: 2011-09-18 09.38.20.390 my binary search will not find an exact match – but i don’t need exact match, i need to find the closest date to it (there is my position).

The current code will jump between 2011-09-18 09.38.20.245 and 2011-09-18 09.38.20.393.

I need some help how to modify the below code so that i get the closest number. In the above situation i would like to have: 2011-09-18 09.38.20.245 (better more than less)

BinarySearch::BinarySearch(QString strFileName,QDateTime dtFrom_target,QDateTime dtTo_target)
{

    QFile file(strFileName);
    qint64 nFileSize = file.size();

    int nNewFromPos;
    int nNewToPos;

    nNewFromPos = Search(file, dtFrom_target, nFileSize);
    nNewToPos  = Search(file, dtFrom_target, nFileSize);

    if(nNewFromPos!=-1 && nNewToPos!=-1){
        // now parse the new range

    }
    else{
        // dates out of bound

    }   
}

int BinarySearch::Search(QFile &file, QDateTime dtKey, int nMax) 
{  
    file.open(QIODevice::ReadOnly);
    char lineBuffer[1024];  
    qint64 lineLength;
    QDateTime dtMid;        
    int mid;
    int min; 
    if(!min) min = 0;


   while (min <= nMax) 
   {
       mid=(min+nMax)/2;    // compute mid point.                               
       file.seek(mid);  // seek to middle of file (position based on bytes)
       qint64 lineLength=file.readLine(lineBuffer,sizeof(lineBuffer)); // read until \n or error

       if (lineLength != -1) //something is read
       {
           // validate string begin (pos = 0) starts with date

           lineLength = file.readLine(lineBuffer, 24); //read exactly enough chars for the date from the beginning of the log file


           if(lineLength == 23)
           {
            dtMid = QDateTime::fromString(QString(lineBuffer),"yyyy-MM-dd HH.mm.ss.zzz"); //2011-09-15 09.38.20.192

                if(dtMid.isValid())
                {
                    if(dtKey > dtMid){
                        min = mid + 1; 
                    }
                    else if(dtKey < dtMid){
                        max = mid - 1; // repeat search in bottom half.
                    }
                    else{
                        return mid;     // found it. return position
                    }
                }
            }
       }
   }
   return -1;    // failed to find key
}
  • 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-25T20:08:51+00:00Added an answer on May 25, 2026 at 8:08 pm

    Try to implement an algorithm equivalent to std::equal_range which returns a pair of result of std::lower_bound and std::upper_bound

    1. Find the position of the first element in the sorted range which does not compare less than the value (lower bound)
    2. Find the position of the first element in the sorted range which compares greater than the value (upper bound)

    —

    template<typename OutIter>
    void ReadLogsInRange(
        std::istream& logStream, Log::Date from, Log::Date to, OutIter out)
    {
        Log l = LowerBound(logStream, from);
        *out++ = l;
        while(logStream >> l && l.date < to)
            *out++ = l;
    }
    

    Full example: http://ideone.com/CvIYm

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

Sidebar

Related Questions

1-)For sorted array I have used Binary Search. We know that the worst case
I have a program that searches a file (numbers.dat) using a binary search and
I have a 384MB text file with 50 million lines. Each line contains 2
I have a file with consisted of int;int values in each line. Both columns
I have a binary search loop which gets hit many times in the execution
I have a PHP script that builds a binary search tree over a rather
I have a scenario where I need to search from many binary files (using
I have binary data in a file that I can read into a byte
I have a binary file that I have to parse and I'm using Python.
I have a binary file - Windows static library (*.lib). Is there a simple

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.