简体   繁体   中英

C++ File input/output strange behavior of the variable

#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

The problem comes from this line :

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

nodesize equals 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.

Not clear what you are trying to achieve, but in your code you have specified.

long  p=0;
while(currentpos>=0)
{

....
p--; // this will make p = -1

}

so the p will print as -1 all through the while statement. Are you forgetting to initialize the p variable?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM