forked from 54shady/linuxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_bin_write.c
More file actions
44 lines (34 loc) · 791 Bytes
/
structured_bin_write.c
File metadata and controls
44 lines (34 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#define MAX_LINE 40
#define FILE_NAME "myfile.bin"
typedef struct {
int id;
float x_coord;
float y_coord;
char name[MAX_LINE+1];
} MY_TYPE_T;
#define MAX_OBJECTS 3
MY_TYPE_T objects[MAX_OBJECTS] = {
{ 0, 1.5, 8.4, "First-object" },
{ 1, 9.2, 7.4, "Second-object" },
{ 2, 4.1, 5.6, "Final-object" }
};
int main(int argc, char *argv[])
{
FILE *fout;
/* Open the output file */
fout = fopen(FILE_NAME, "w");
if ((FILE *)0 == fout) {
/* Emit an error message and exit */
printf("%s: %s\n", FILE_NAME, strerror(errno));
exit(-1);
}
/* Write out the entire objects structure */
fwrite((void *)objects, sizeof(MY_TYPE_T), MAX_OBJECTS, fout);
fclose(fout);
return 0;
}