-1

For my school project, i am creating a game. For save game features, I am using a .DAT binary file to read and write the data. When I enter the necessary details, the file doesn't have any data stored in it after exiting the program.

void save_game()
{
    savegame sg;

    fstream x("savegame.dat", ios::out|ios::binary);
    char pn[25];

    sg.points = point;

    sg.lives = count;

    clrscr();

    settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3);

    outtextxy(50, 50, "ENTER YOUR NAME,PLAYA!");

    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

    gets(pn);

    strcpy(sg.pname, pn);

    x.write((char*)&sg, sizeof(sg));

    quitgame();
}

When I open the .DAT file, I expect to have some data stored in it. However, the file remains empty.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

4

You've not provided a compilable verifiable example, so I can only use my crystal ball to help you. In the crystal ball I see that quitgame() calls exit(), in which case the destructor of your x doesn't get called, so the file isn't closed nor flushed to disk. Thus it remains empty.

See also a related question and links therein: Why is using exit() considered bad?

Ruslan
  • 18,162
  • 8
  • 67
  • 136