From 134f855b537b714eb98bc1e6e1e4790b4d6491a3 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sat, 22 Jun 2024 20:01:14 +0200 Subject: Added writing and reading structs from files --- c-structs/write.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 c-structs/write.c (limited to 'c-structs/write.c') diff --git a/c-structs/write.c b/c-structs/write.c new file mode 100644 index 0000000..e83531a --- /dev/null +++ b/c-structs/write.c @@ -0,0 +1,25 @@ +#include +#include + +#include "struct.h" + +int main(void) { + printf("Write struct\n"); + + Character ch; + + strcpy(ch.name, "John Doe"); + ch.health = 30; + ch.damage = 5.9; + + FILE* file = fopen("character.dat", "wb"); + if (file == NULL) { + perror("Error opening file"); + return 1; + } + + fwrite(&ch, sizeof(Character), 1, file); + fclose(file); + + return 0; +} -- cgit v1.2.3