Write using unistd.h

I have this program that is basically a mini game played in the terminal where the player is in a dungeon and tries to kill a dragon. I have a function that tries to save the game when the player chooses too and i see that its not writing the correct input, thus causing the load of that saved file to crash with a segmentation fault. I used hexdump to see that indeed the input I want to save isn’t correct (I have a saved file that has been given to me with the correct contents for a specific game). The following is my save_game function and relevant structs to the game. Can someone help me identify the issue?

int save_game(const graph_t *graph, const player_t *player) 
{
    // Create the file's name which is basically the player's name with a ".sav" extension to it
    char filename[NAME_SIZE];
    snprintf(filename, sizeof(filename), "%s.sav", player->name);

    // I open the file to write on it after creating it, or if it already exists I overwrite it
    int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1) return -1;

    /* Write the magic number which is a specific saved number that i write in the beggining and end of the file to identify them */

    if(write(fd, MAGIC_NUMBER, sizeof(MAGIC_NUMBER) - 1) != sizeof(MAGIC_NUMBER)-1) // χωρίς το null terminator
    {
        close(fd);
        return -1;
    }


    // Write player's details
    if (write(fd, &player->arrows, sizeof(player->arrows)) != sizeof(player->arrows) ||
        write(fd, &player->room->id, sizeof(player->room->id)) != sizeof(player->room->id)) 
    {
        close(fd);
        return -1;
    }

    // Write graph's details (The graph is basically the dungeon)
    if (write(fd, &graph->max_id, sizeof(graph->max_id)) != sizeof(graph->max_id) ||
        write(fd, &graph->num_vertices, sizeof(graph->num_vertices)) != sizeof(graph->num_vertices)) 
    {
        close(fd);
        return -1;
    }

    // Write every vertice's details (each vertex has an edge that leads to other vertices)
    for (unsigned int i = 0; i < graph->num_vertices; i++) 
    {
        vertex_t *vertex = graph->vertices[i];
        if (write(fd, &vertex->id, sizeof(vertex->id)) != sizeof(vertex->id) ||
            write(fd, &vertex->contents, sizeof(vertex->contents)) != sizeof(vertex->contents)) {
            close(fd);
            return -1;
        }
        unsigned int count = 0;
        for (edge_t *edge = vertex->edges; edge != NULL; edge = edge->next) 
        {
            count++;
        }

        if (write(fd, &count, sizeof(count)) != sizeof(count)) 
        {
            close(fd);
            return -1;
        }
        for (edge_t *edge = vertex->edges; edge != NULL; edge = edge->next) 
        {
            if (write(fd, &edge->to->id, sizeof(edge->to->id)) != sizeof(edge->to->id)) 
            {
                close(fd);
                return -1;
            }
        }
    }

    // Write the magic number again at the end of the file
    if (write(fd, MAGIC_NUMBER, sizeof(MAGIC_NUMBER) - 1) != sizeof(MAGIC_NUMBER) - 1) {
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}
typedef struct {
    char name[NAME_SIZE];
    unsigned int arrows;
    vertex_t *room;
} player_t;

typedef struct  _vertex {
    unsigned int id; // unique id
    contents_t contents;
    struct _edge *edges; // a vertex connects to a list of edges
} vertex_t;

typedef struct _edge {
    vertex_t *to;  // the other adjacent vertex
    struct _edge *next;
} edge_t;
    
typedef struct {
    unsigned int max_id; // maximum vertex id given at any time
    unsigned int num_vertices;
    vertex_t **vertices;  // the graph is a dynamic array of pointers to vertices
} graph_t;

the following is the output i got from the hexdump after I compared the file’s content i got with the desired content of the saved file. Note that “Dr460N5D3N” is the magic number. The first is the file i created and the second one is the desired output.

2,23c2,24
< 00000010  00 00 0c 00 00 00 0d 00  00 00 01 00 00 00 00 00  |................|
< 00000020  00 00 03 00 00 00 0a 00  00 00 0b 00 00 00 0c 00  |................|
< 00000030  00 00 02 00 00 00 02 00  00 00 04 00 00 00 0d 00  |................|
< 00000040  00 00 08 00 00 00 09 00  00 00 0c 00 00 00 03 00  |................|
< 00000050  00 00 00 00 00 00 03 00  00 00 0b 00 00 00 07 00  |................|
< 00000060  00 00 0c 00 00 00 04 00  00 00 00 00 00 00 03 00  |................|
< 00000070  00 00 06 00 00 00 07 00  00 00 09 00 00 00 05 00  |................|
< 00000080  00 00 02 00 00 00 03 00  00 00 06 00 00 00 07 00  |................|
< 00000090  00 00 09 00 00 00 06 00  00 00 01 00 00 00 03 00  |................|
< 000000a0  00 00 0a 00 00 00 05 00  00 00 04 00 00 00 07 00  |................|
< 000000b0  00 00 04 00 00 00 03 00  00 00 03 00 00 00 05 00  |................|
< 000000c0  00 00 04 00 00 00 08 00  00 00 00 00 00 00 03 00  |................|
< 000000d0  00 00 0b 00 00 00 0a 00  00 00 02 00 00 00 09 00  |................|
< 000000e0  00 00 03 00 00 00 04 00  00 00 0d 00 00 00 05 00  |................|
< 000000f0  00 00 04 00 00 00 02 00  00 00 0a 00 00 00 01 00  |................|
< 00000100  00 00 03 00 00 00 08 00  00 00 06 00 00 00 01 00  |................|
< 00000110  00 00 0b 00 00 00 00 00  00 00 03 00 00 00 03 00  |................|
< 00000120  00 00 08 00 00 00 01 00  00 00 0c 00 00 00 01 00  |................|
< 00000130  00 00 03 00 00 00 03 00  00 00 02 00 00 00 01 00  |................|
< 00000140  00 00 0d 00 00 00 00 00  00 00 02 00 00 00 09 00  |................|
< 00000150  00 00 02 00 00 00 44 72  34 36 30 4e 35 44 33 4e  |......Dr460N5D3N|
< 00000160
---
> 00000010  00 00 0d 00 00 00 0d 00  00 00 44 72 34 36 30 4e  |..........Dr460N|
> 00000020  35 44 33 4e 01 00 00 00  00 00 00 00 02 00 00 00  |5D3N............|
> 00000030  02 00 00 00 03 00 00 00  00 00 00 00 04 00 00 00  |................|
> 00000040  00 00 00 00 05 00 00 00  02 00 00 00 06 00 00 00  |................|
> 00000050  01 00 00 00 07 00 00 00  04 00 00 00 08 00 00 00  |................|
> 00000060  00 00 00 00 09 00 00 00  03 00 00 00 0a 00 00 00  |................|
> 00000070  01 00 00 00 0b 00 00 00  00 00 00 00 0c 00 00 00  |................|
> 00000080  01 00 00 00 0d 00 00 00  00 00 00 00 03 00 00 00  |................|
> 00000090  0a 00 00 00 0b 00 00 00  0c 00 00 00 04 00 00 00  |................|
> 000000a0  0d 00 00 00 08 00 00 00  09 00 00 00 0c 00 00 00  |................|
> 000000b0  03 00 00 00 0b 00 00 00  07 00 00 00 0c 00 00 00  |................|
> 000000c0  03 00 00 00 06 00 00 00  07 00 00 00 09 00 00 00  |................|
> *
> 000000e0  03 00 00 00 0a 00 00 00  05 00 00 00 04 00 00 00  |................|
> 000000f0  03 00 00 00 03 00 00 00  05 00 00 00 04 00 00 00  |................|
> 00000100  03 00 00 00 0b 00 00 00  0a 00 00 00 02 00 00 00  |................|
> 00000110  04 00 00 00 0d 00 00 00  05 00 00 00 04 00 00 00  |................|
> 00000120  02 00 00 00 03 00 00 00  08 00 00 00 06 00 00 00  |................|
> 00000130  01 00 00 00 03 00 00 00  03 00 00 00 08 00 00 00  |................|
> 00000140  01 00 00 00 03 00 00 00  03 00 00 00 02 00 00 00  |................|
> 00000150  01 00 00 00 02 00 00 00  09 00 00 00 02 00 00 00  |................|
> 00000160  44 72 34 36 30 4e 35 44  33 4e                    |Dr460N5D3N|
> 0000016a

I tried hexdump and some lldb but i couldnt really pinpoint the issue. The sanitizer also didnt crash when saving.

New contributor

user23903249 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật