I'm attempting to create a FAT file system I understand the basic principle of how its supposed to set up and I'm using a struct like this for each FAT entry
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 */
};
I'm essentially creating a 2 MB file to act as my file system. From there I will write and read files into blocks of 512 bytes each. My question how I can write a struct to a file? Does fwrite allow me to do this? For example:
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);
Will that store the struct in bytes? How do I read from this? I'm having trouble understanding what I will get back when I use fread
Will that store the struct in bytes?
&entry1
to (void*)
How do I read from this?
fread((void*)&entry1,sizeof(entry1),1,file);
(but don't forget "r" flag to fopen()
)
The real problem in your case is that the struct will probably be padded by the compiler, for efficient access. so you will have to use __attribute__((packed))
if you are using gcc.
[EDIT] code sample (C, not C++):
struct FATEntry entry1 { "abc", 3, 10, 100 };
FILE* file1 = fopen("filesys", "wb");
fwrite(&entry1, sizeof(struct FATEntry), 1, file1);
fclose(file1)
struct FATEntry entry2 { "", 0, 0, 0 };
FILE* file2 = fopen("filesys", "rb");
fread(&entry2, sizeof(struct FATEntry), 1, file2;
fclose(file2)
You can now check that you read what you have written earlier:
assert(memcmp(&entry1, &entry2, sizeof(struct FATEntry))==0);
the assert will fail if the read or the write did not succeed (I didn't check for this).