Will that store the struct in bytes?
- yes. in C++ you will need to explicitly cast
&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).