www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I serialize a struct into a file in the style of C?

reply solidstate1991 <laszloszeremi outlook.com> writes:
Due to it's convenience, I was thinking on reading and writing 
file headers by creating structs mirroring the layouts of actual 
headers I would need. I've seen many examples of this in C, 
however I' struggling using the same methods through the use of 
code.stdc.stdio, especially as I can't really trace bugs from 
fread.
Jul 21 2017
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 22 July 2017 at 01:45:29 UTC, solidstate1991 wrote:
 Due to it's convenience, I was thinking on reading and writing 
 file headers by creating structs mirroring the layouts of 
 actual headers I would need. I've seen many examples of this in 
 C, however I' struggling using the same methods through the use 
 of code.stdc.stdio, especially as I can't really trace bugs 
 from fread.
struct Data { int x; float y; ubyte z; } void main() { import core.stdc.stdio; Data od = Data(10, 3.0f, 5); FILE* fp = fopen("data.dat", "wb"); size_t ret = fwrite(&od, od.sizeof, 1, fp); fclose(fp); assert(ret == 1); Data id; fp = fopen("data.dat", "rb"); ret = fread(&id, id.sizeof, 1, fp); fclose(fp); assert(ret == 1); assert(id.x == 10); assert(id.y == 3.0f); assert(id.z == 5); }
Jul 21 2017
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 22 July 2017 at 02:11:27 UTC, Mike Parker wrote:
 On Saturday, 22 July 2017 at 01:45:29 UTC, solidstate1991 wrote:
 Due to it's convenience, I was thinking on reading and writing 
 file headers by creating structs mirroring the layouts of 
 actual headers I would need. I've seen many examples of this 
 in C, however I' struggling using the same methods through the 
 use of code.stdc.stdio, especially as I can't really trace 
 bugs from fread.
struct Data { int x; float y; ubyte z; } void main() { import core.stdc.stdio; Data od = Data(10, 3.0f, 5); FILE* fp = fopen("data.dat", "wb"); size_t ret = fwrite(&od, od.sizeof, 1, fp); fclose(fp); assert(ret == 1); Data id; fp = fopen("data.dat", "rb"); ret = fread(&id, id.sizeof, 1, fp); fclose(fp); assert(ret == 1); assert(id.x == 10); assert(id.y == 3.0f); assert(id.z == 5); }
I should add, though, that you're better off using either std.stdio.File or std.file. Use the former if you need to make multiple reads/writes to a file, the latter if you can pull it in or push it out all in one go. They take arrays as arguments, so if you have something like Data[], you can pass it directly to the appropriate functions. To write a single instance, you'll have to take the pointer and slice it. Either way, it's less code, less error prone, and more idiomatic than using the C API.
Jul 21 2017
parent reply solidstate1991 <laszloszeremi outlook.com> writes:
On Saturday, 22 July 2017 at 02:22:53 UTC, Mike Parker wrote:
 I should add, though, that you're better off using either 
 std.stdio.File or std.file. Use the former if you need to make 
 multiple reads/writes to a file, the latter if you can pull it 
 in or push it out all in one go. They take arrays as arguments, 
 so if you have something like Data[], you can pass it directly 
 to the appropriate functions. To write a single instance, 
 you'll have to take the pointer and slice it. Either way, it's 
 less code, less error prone, and more idiomatic than using the 
 C API.
Well, it seems the toStringz() function adds a few garbage characters to the end of the filename, I might look into a way to read the data with Phobos instead of the C API.
Jul 22 2017
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 23 July 2017 at 02:07:45 UTC, solidstate1991 wrote:
 Well, it seems the toStringz() function adds a few garbage 
 characters to the end of the filename
How are you using it? The only character it should be adding is the zero terminator. I suspect you might be using it on a static array without slicing it down to size first...
Jul 22 2017
prev sibling parent Temtaime <temtaime gmail.com> writes:
Hi !
I have a dub package that doing this.

https://github.com/Temtaime/tt-utils/blob/master/source/tt/binary/tests.d

Have a look at the tests.
Currently it has no documentation, but feel free to ask questions
Jul 23 2017