2012-04-01 66 views
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(); 

} 

該代碼只是讀取和寫入二進制文件。在第一部分它寫入一個文件,第二部分從同一個文件讀取。閱讀時,我試圖從文件中讀取有限次數的任意塊。但是當我使用p變量作爲計數器時,它不起作用。它的值在第一次迭代中直接遞減到-1。我使用調試器來跟蹤它的變化。顯然,它在讀取語句執行後會改變。有人可以幫助我嗎?上述程序的輸出是這個C++文件輸入/輸出變量的奇怪行爲

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 

回答

0

的問題來自於這一行:

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

nodesize等於256,而你結構的大小,如果88byte(22 * 4個字節INT)。

讀取正在將內存寫入結構,恰好是其他堆棧變量。

使用sizeof(node)當你寫入和讀取結構到文件。

+0

謝謝!它現在有效。 – user1306082 2012-04-01 09:28:42

0

不清楚你想達到什麼,而是在你指定的代碼中。

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

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

} 

因此,p將通過while語句打印爲-1。你忘記初始化p變量了嗎?

+0

我只是想改變p的值。但它並沒有改變,並被固定爲一個值。可能是由於crazyjul給出的上述原因 – user1306082 2012-04-01 09:50:45