create binary file

I'm trying to get a binay file open to save the high scores of the game I'm writing (with allegro, if you want to know) . Heres how I'm doing it. (posted in order of calling)

[global]

fstream sav;

[main()]

sav.open("hssav.sav", ios::binary|ios::in|ios::out);

[game() function called by main()]

textprintf(buffer, font, SCREENW / 2, 30, BLUE, "Opened %d", ((int)sav.is_open()));

//prints the integer result of (int)sav.is_open()

[exit(function called by main)]

sav.close();

(int)sav.is_open is returning 0, I think that means that the object couldn't open the file. Am I right Assuming I am, why would that be Doesn't open(...) create the file if it doesn't exist. If not, how do I go about creating the file




Answer this question

create binary file

  • J_o_n_a_t_h_a_n

    Correct.

    !sav.fail()

    Kuphryn


  • murphymom

    what I want to do is this.

    1) Look for "hssav.sav" and read the highscores for display, If it doesn't exist, create it with 0s for high scores.

    2) Display the scores throughout the game.

    3) When the player dies (and they will, ha ha), compare their scores. If the player did better than the last high score, write his score to "hssav.sav".

    If I can only open the fstream object in ios::out and ios::binary, will I be able to read from it Maybe I should create more than one object



  • Andre923

    ios::in stipulates that the file exists, since you're opening it for reading. Remove it, and you're good to go.


  • create binary file