C++ 文件输入/输出变量的奇怪行为
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();
}
此代码只是读取和写入二进制文件。在第一部分中,它写入文件,在第二部分中,它从同一文件读取。在阅读时,我试图在有限的时间内从文件中读取任何随机块。但是当我使用 p 变量作为计数器时,它不起作用。它的值在第一次迭代中直接递减为 -1。我使用调试器来跟踪它的变化位置。显然,它在执行 read 语句后会发生变化。有人可以帮我解决这个问题吗?上述程序的输出是这样的
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
问题来自这一行:
myfile.read(reinterpret_cast<char *>(&node),nodesize);
nodesize
等于 256,而结构的大小如果为 88 字节(22 * 4 字节 int(。
读取是在结构上写入内存,这恰好是其他堆栈变量。
在向文件写入和读取结构时,请使用sizeof( node )
。
不清楚您要实现的目标,但是在您的代码中您已经指定了。
long p=0;
while(currentpos>=0)
{
....
p--; // this will make p = -1
}
因此,P 将在整个 while 语句中打印为 -1。你忘了初始化 p 变量吗?
相关文章:
- 通过多个头文件使用常量变量
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 无法通过头文件保存变量的值
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在 C++ 中将双精度变量写入二进制文件
- 如何使用文件中的文本,例如变量
- 定义C++新的环境变量并在 bat 文件中使用它
- 在头文件中定义变量不会出错
- 复制文件流C++静态变量
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- C++将文本从.txt文件解析为变量
- 从 C 中的变量获取文件的路径,C++
- 可以在 cpp 文件中C++定义为特殊符号的变量β
- 如何使用可视化代码和平台IO将环境变量注入CPP文件?
- C++ 静态成员变量 - 文件之间的访问
- 正在创建一个集中式变量文件
- 静态成员变量文件作用域