How to write struct to binary file in C

1 Answer

0 votes
#include <stdio.h>

typedef struct Point {
    int x, y;
} Point;
 
int main(void) {
	Point p = {
        .x = 89731,
        .y = 26
    };
    
    FILE *out = fopen("data.bin", "wb");
    if (out == NULL) {
        return 1;
    }
    
    size_t total_written = fwrite(&p, sizeof(Point), 1, out);
    fclose(out);
	
    if (total_written == 0) {
        return 1;
    }
 
    return 0;
}

 
 
/*
point.bin
 
// ƒ^    
 
*/
 
 
 
/*
run:
 
   
*/

 



answered Dec 29, 2020 by avibootz
edited Dec 30, 2020 by avibootz

Related questions

1 answer 154 views
154 views asked Jun 23, 2017 by avibootz
1 answer 263 views
1 answer 222 views
1 answer 169 views
169 views asked Dec 30, 2020 by avibootz
2 answers 271 views
271 views asked Dec 30, 2020 by avibootz
1 answer 258 views
1 answer 204 views
...