Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图在C++;?_C++_Filesystems_Fat - Fatal编程技术网

C++ 试图在C++;?

C++ 试图在C++;?,c++,filesystems,fat,C++,Filesystems,Fat,我正在尝试创建一个FAT文件系统,我了解它应该如何设置的基本原理,并且我正在为每个FAT条目使用这样的结构 struct FATEntry { char name[20]; /* Name of file */ uint32_t pos; /* Position of file on disk (sector, block, something else) */ uint32_t size; /* Size in bytes of f

我正在尝试创建一个FAT文件系统,我了解它应该如何设置的基本原理,并且我正在为每个FAT条目使用这样的结构

struct FATEntry
{
    char      name[20];  /* Name of file */
    uint32_t  pos;       /* Position of file on disk (sector, block, something else) */
    uint32_t  size;      /* Size in bytes of file */
    uint32_t  mtime;     /* Time of last modification */
};
我基本上创建了一个2MB文件作为我的文件系统。从那里我将把文件写入和读取到每个512字节的块中。我的问题是如何将结构写入文件?弗里特允许我这么做吗?例如:

struct FATEntry entry1;
strcpy(entry1.name, "abc");
entry1.pos = 3;
entry1.size = 10;
entry1.mtime = 100;
cout << entry1.name;

file = fopen("filesys", "w");
fwrite(&entry1,sizeof(entry1),1,file);
fclose(file);
struct FATEntry entry1;
strcpy(entry1.name,“abc”);
entry1.pos=3;
entry1.size=10;
entry1.mtime=100;

这会以字节为单位存储结构吗

  • 对。在C++中,您需要明确地将<代码>和Cytoy1<代码>转换为<代码>(空洞*)< /Calp>
我该如何解读这些

  • fread((void*)&entry1,sizeof(entry1),1,file)
(但别忘了将“r”标志设为
fopen()

在您的案例中,真正的问题是,为了高效访问,struct。因此,如果您使用的是gcc,则必须使用
\uuuuuuuuuuuuuuu属性((打包))

[编辑] 代码示例(C,不是C++):

现在,您可以检查您是否阅读了之前所写的内容:

assert(memcmp(&entry1, &entry2, sizeof(struct FATEntry))==0);

如果读取或写入未成功,则将失败(我没有对此进行检查)。

谢谢,但是fread((void*)和entry1,sizeof(entry1),1,file有什么作用;返回?结构?我想这就是让我困惑的地方。返回“成功读取的元素总数”。这可能比你想的要少。它将用实际数据填充
entry1
。哎呀,我忘了说你也需要使用“b”标志,你是在处理原始数据,而不是文本。
assert(memcmp(&entry1, &entry2, sizeof(struct FATEntry))==0);