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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:48:01+00:00 2026-05-25T09:48:01+00:00

In this program, I am reading key.pc.db file and printing its mid value. #include

  • 0

In this program, I am reading “key.pc.db” file and printing its mid value.

#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
        ifstream  fp;     
        int mval;
        int sizek;
        struct stat filek;
        int min, max, mid;
        if(stat("key.pc.db", &filek) ==0 )
             sizek=filek.st_size;
        sizek=sizek/sizeof(int);
        min=0;
        max=sizek-1;
        mid=(min+max)/2;    
        printf("mid %d ",mid);
        fp.open( "key.pc.db", ios::in | ios::binary ); 
        fp.seekg(mid, ios::beg);
        fp.read( (char *) &mval, (int) sizeof( int ) );
        printf("%d mval ", mval);
        getch();
        return 1;
}

In this program also I am reading the same file but I am storing the value of the file in an array and then printing the mid value. The mid index for both the program shows the same but the value comes out to be different. Why so?

#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
        ifstream  fp;     
        int index;
        int sizek;
        int kval;
        struct stat filek;
        int min, max, mid;
        int i=0;
        if(stat("key.pc.db", &filek) ==0 )
             sizek=filek.st_size;
        sizek=sizek/sizeof(int);
        int k[sizek];
        fp.open( "key.pc.db", ios::in | ios::binary );  
        fp.read( (char *) &kval, (int) sizeof( int ) );
        while( !fp.eof() ) 
         {
               k[i++]=kval;
               fp.read( (char *) &kval, (int) sizeof( int ) );
         }
        min=0;
        max=sizek-1;
        mid=(min+max)/2;
        printf(" index %d ", mid);
        printf(" kmid %d ", k[mid]);
        getch();
        return 1;
}
  • 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-25T09:48:02+00:00Added an answer on May 25, 2026 at 9:48 am

    You’re seeking to the wrong point in the file in the first case. You should be seeking to mid*sizeof(int) rather than mid.

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

Sidebar

Related Questions

I'm writing a program. Part of this program involves reading in a unicode value
I got this error Unhandled exception at 0x0049b946 in Program.exe: 0xC0000005: Access violation reading
This program reads emails (really just a .txt file structured like an email) and
For this program #include <iostream> using std::cout; struct C { C() { cout <<
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
I have this program that is reading the test string from a textbox and
This is my first python program - Requirement: Read a file consisting of {adId
I have some trouble reading from an ini file using boost program options. The
I made this little program while reading Head First C# . I'm trying to
I have a program that is very heavily hitting the file system, reading and

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.