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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:38:58+00:00 2026-06-01T05:38:58+00:00

#include<iostream> #include<time.h> #include<list> #include<stdlib.h> #include<fstream> using namespace std; typedef struct diskBtNode { int parent;

  • 0
#include<iostream>
#include<time.h>
#include<list>
#include<stdlib.h>
#include<fstream>
using namespace std;

typedef struct diskBtNode
{
    int parent; //-1 if NULL
    //int size;
    int leaf;
    int arr[20];
};

int main()
{

    fstream myfile;
    srand(time(NULL));
    myfile.open("btree.txt",ios::in | ios::out | ios::binary | ios::trunc);
    long nodesize=256;
    long currentpos=0;
    if(myfile.fail())
    {
        std::cout<<"Error opening the file "<<std::endl;
    }
    currentpos=0;
    for(int i=0;i<10;i++)
    {
        diskBtNode node;
        node.parent=rand()%10;
        node.leaf=rand()%1;
        int n=rand()%19;
        int j;
        for(j=0;j<n;j++)
        {
            node.arr[j]=n;
        }
        node.arr[j]=-1;

        cout<<node.parent<<" "<<node.leaf<<" ";
        j=0;
        while(node.arr[j]!=-1)
        {
        cout<<node.arr[j]<<" ";
        j++;
        }
        cout<<node.arr[j]<<std::endl;

        myfile.seekp(currentpos*nodesize,ios::beg);
        myfile.write(reinterpret_cast<char *>(&node),nodesize);
        currentpos++;
//      p=p+1;
    }


    cout<<"******************* "<<std::endl;
    currentpos--;
    long  p=0;
    while(currentpos>=0)
    {
        std::cout<<currentpos<<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "<<p<<" "<<std::endl;
        diskBtNode node;
        myfile.seekg(currentpos*nodesize,ios::beg);
        myfile.read(reinterpret_cast<char *>(&node),nodesize);
        currentpos--;
        p--; //decrementing p
        cout<<node.parent<<" "<<node.leaf<<" ";
        int j=0;
        while(node.arr[j]!=-1)
        {
        cout<<node.arr[j]<<" ";
        j++;
        }
        cout<<node.arr[j]<<std::endl;

    }

    myfile.close();

}

This code simply reads and writes to a binary file. In the first part it writes to a file and in the second part it reads from the same file. While reading I was trying to read any random blocks from a file for a finite number of time. But when I am using p variable as a counter, it doesn’t work. It’s value is decremented in the first iteration directly to -1. I used debugger to track where it changes. Apparently it changes after the read statement is executed. Can somebody please help me with this? The output of the above program is this

8 0 8 8 8 8 8 8 8 8 -1
5 0 8 8 8 8 8 8 8 8 -1
3 0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 -1
5 0 1 -1
4 0 -1
9 0 13 13 13 13 13 13 13 13 13 13 13 13 13 -1
4 0 11 11 11 11 11 11 11 11 11 11 11 -1
6 0 6 6 6 6 6 6 -1
6 0 8 8 8 8 8 8 8 8 -1
2 0 2 2 -1
******************* 
9 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 0 
2 0 2 2 -1
8 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
6 0 8 8 8 8 8 8 8 8 -1
7 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
6 0 6 6 6 6 6 6 -1
6 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
4 0 11 11 11 11 11 11 11 11 11 11 11 -1
5 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
9 0 13 13 13 13 13 13 13 13 13 13 13 13 13 -1
4 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
4 0 -1
3 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
5 0 1 -1
2 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
3 0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 -1
1 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
5 0 8 8 8 8 8 8 8 8 -1
0 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1 
8 0 8 8 8 8 8 8 8 8 -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-06-01T05:39:00+00:00Added an answer on June 1, 2026 at 5:39 am

    The problem comes from this line :

    myfile.read(reinterpret_cast<char *>(&node),nodesize);
    

    nodesizeequals 256, while you structure’s size if 88byte ( 22 * 4 bytes int ).

    The read is writing memory over the structure, which happens to be the other stack variables.

    Use sizeof( node ) when you both write and read the struct to the file.

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

Sidebar

Related Questions

#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int twoify(int num, int times)
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include<iostream> using namespace std; struct sample { int data[3][2]; }; struct sample* function() {
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
My code is the following: #include <iostream> #include <sys/time.h> using namespace std; int main(int
here is algorithm #include<iostream> #include<Windows.h> #include<time.h> using namespace std; int main(){ int a[10]={12,3,5,2,7,80,10,1,16,30}; long
Well, not random, because its the same every time, but #include<iostream> using namespace std;
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int

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.