Skip to content

Instantly share code, notes, and snippets.

@mahadirz
Last active January 22, 2022 05:23
Show Gist options
  • Save mahadirz/b134a8162afb9c8b5f9f59b12ec5233e to your computer and use it in GitHub Desktop.
Save mahadirz/b134a8162afb9c8b5f9f59b12ec5233e to your computer and use it in GitHub Desktop.
mysql_binlog_tutorial
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int id;
uint32_t age;
char firstName[32];
char lastName[32];
} Person;
int main()
{
FILE *fd;
// open file for writing
fd = fopen("person.bin", "w");
if (fd == NULL)
{
fprintf(stderr, "\nError opened file\n");
exit(1);
}
Person p1 = {1, 20, "Mahadir", "Ahmad"};
Person p2 = {2, 50, "Albert", "Einstein"};
Person p3 = {3, 80, "Albus", "Dumbledore"};
// Save the struct into the opened file
fwrite(&p1, sizeof(Person), 1, fd);
fwrite(&p2, sizeof(Person), 1, fd);
fwrite(&p3, sizeof(Person), 1, fd);
// close file
fclose(fd);
// open file for reading
fd = fopen("person.bin", "r");
if (fd == NULL)
{
fprintf(stderr, "\nError opened file\n");
exit(1);
}
Person person;
printf("sizeof(Person): %ld \n", sizeof(Person));
while (fread(&person, sizeof(Person), 1, fd))
{
printf("%d %d %s %s\n", person.id,
person.age, person.firstName, person.lastName);
}
fclose(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment