将缓冲区写入结构似乎没有起作用
Writing a buffer to a struct seems to not be working
我正在研究一个旨在为类创建简单版本的项目。当我使用我们定义的" Inode"结构时,我会遇到麻烦。Inode结构定义如下:
struct iNode
{
char name[8]; //file name
int size; // file size
int blockPointers[8]; // direct block pointers
int used; // 0 if free; 1 if in use
};
我的项目稍后也有以下代码:
char* inodes = NULL;
inodes = new char[48];
iNode *myinode = new iNode();
int inodemax = 16;
int storedi = 0;
int x = 0;
while(x < inodemax){
disk.seekg(128 + 48*x,ios::beg);
disk.read(inodes, 48);
inodex = (struct iNode *) &inodes;
if(inodex -> used == 0){
inodex -> used = 1;
for(int i = 0; i < 8; i++){
inodex -> name[i] = name[i];
}
inodex -> size = size;
storedi = disk.tellg();
break;
}
if(inodex -> used != 0 && x == 15){
return 4;
}
x = x +1;
}
我不太精通C ,因此任何明显的错误都可能以我对语言知识有限(我对C的经验更好)为代价。我的问题是因为我创建了一个测试用例,其中使用的inodes应该为0-但是返回" 4"的返回语句。它不应该达到代码的那一部分。
当我手动更改代码以从Inodes缓冲区获取"二手"字段时,它可以正常工作。因此,我被认为我在将我的char*缓冲区复制到我的结构中做错了什么。
任何人都可以确定我在做什么错以及如何解决它?谢谢,我已经被困了一段时间了。
inodes = new char[48];
inodes
是一个指向48个字节的缓冲区的指针。
inodex = (struct iNode *) &inodes;
这将inodex
设置为指向inodes
指针。不是inodes
指的是指向指针本身。
很明显这应该是:
inodex = (struct iNode *) inodes;
或更明确:
inodex = reinterpret_cast<struct iNode *>(inodes);
相关文章:
- 编译器上的策略数据结构不起作用
- C++结构模板变量快捷方式定义不起作用
- 内部结构初始化不起作用 - C++
- 没有类型结构绑定不起作用?
- 使用选择排序对数组数据结构进行排序,但它不起作用
- 结构/方法不起作用
- 如果在结构内部使用condition_variable,则它不起作用
- C++覆盖父结构中的数据不起作用
- 将参考类型作为嵌套模板结构中的模板参数作为模板参数不起作用
- 指向结构的指针不起作用
- 结构程序不起作用
- GCC:当层次结构中存在虚拟继承时,C++11 内联对象初始化(使用 "this")不起作用
- C++ -- 指向结构数组的指针,该数组包含指向不起作用的类数组的指针
- 如果所述结构具有构造函数,则带有 new[] 的结构数组似乎不起作用
- 在文档中引用使用doxygen的结构成员不起作用;结构成员未出现在文档中
- 将缓冲区写入结构似乎没有起作用
- 为什么C 11支撑初始化与基于堆栈的类层次结构一起起作用
- 为什么我的结构中映射的构造函数不起作用
- 我的火狐扩展程序不起作用。使用C++XPCOM组件的Firefox扩展(XPI文件)的结构是什么?
- c++ 将文本文件读取到结构数组中不起作用