Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++: using binary files...
#1
I don't want to mess my map file up, so I want this to work the first try. Would the first function work the way I want it to (write all the data inside map[19][550] then write all the data inside each struct array). If I could write all the data of an array of structs in one command, I'd like someone to show me how.

Code:
int save_map(string s) {
   FILE *f;
    f = fopen(s, "w");
    if (!f) return 1;
    fwrite(map, sizeof(map), 1, f);
    for (int i=0;i<MAX_ITEMS;i++) {
        if (i < MAX_ENEMIES) fwrite(enemy[i], sizeof(struct enemy), 1, f);
        fwrite(item[i], sizeof(struct item), 1, f);
    }
    fclose(f);
}
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#2
Your question confuses me from your topic, if you want binary files just open the file with "wb" instead of "w". FILE * is just a stream so basically, if it works non binary, it'll work binary and itll work with the screen as well.

And um, are your structs being defined in lower case? Its good programming practice to define in caps, eg struct ITEMS.

And to write your item in one go, then just do it like you did with the map
[code]
fwrite(item, sizeof(item), 1, f);
[/cpde]
I see no reason why it would no work, becuase the data should still be linear(it is in fact , still linear)... If it doesnt work, you might need a pointer to a pointer......
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply
#3
yeah, sorry bout the topic, and I didn't know that you use caps for structs. The program loads the map right, but not the items or the enemies. Here is the code for the save and load functions. The text version of the save function is at the bottom.

Code:
int load_map(char *s) {
      int i;
    ifstream file_in(s, ios::in | ios::binary);
    if (!file_in.is_open()) return 1;
     file_in.read((char *)map, sizeof(map));
     file_in.read((char *)enemy, sizeof(enemy));
     file_in.read((char *)item, sizeof(item));
    file_in.close();
    for (i=0;i<MAX_ENEMIES;i++) {    
        if (enemy[i].rbuff == 1 || !enemy[i].rbuff) enemy[i].buff = bichi;
        if (enemy[i].rbuff == 2) enemy[i].buff = bichi_green;
        if (enemy[i].rbuff == 3) enemy[i].buff = bichi_blue;
    }
    return 0;
}

int save_map(char *s) {
    ofstream file_out (s, ios::out | ios::binary | ios::trunc);
    if (!file_out.is_open()) return 1;
     file_out.write((char *)map, sizeof(map));
     file_out.write((char *)enemy, sizeof(enemy));
     file_out.write((char *)item, sizeof(item));
    file_out.close();
    return 0;
}
Code:
ofstream file_out;
    file_out.open(s.c_str());
    if (!file_out.is_open()) return 1;
    int total_x = 19, total_y = 549;
    file_out << total_x << " " << total_y << " ";    
    for(int y=0; y!=total_y; y++) for(int x = 0; x!=total_x; x++) file_out << map[x][y] << " ";
    for (int i=0;i<MAX_ITEMS;i++) {
        if (i < MAX_ENEMIES) file_out << enemy[i].hp << " " << enemy[i].x << " " << enemy[i].y << " " << enemy[i].rbuff << " " << enemy[i].speed << " " << enemy[i].ai << " " << enemy[i].dir << " " << enemy[i].frame_wait << " " << enemy[i].sleep << " " << enemy[i].grenades << " " << enemy[i].yen << " " << enemy[i].spell_1 << " " << enemy[i].spell_2 << " " << enemy[i].spell_3 << " ";
        file_out << item[i].tile << " " << item[i].kind << " " << item[i].walk << " " << item[i].frames << " " << item[i].frame_wait << " " << item[i].looped << " " << item[i].x << " " << item[i].y << " ";
    }
    file_out.close();
it's sloppy looking. That's why I want to use binary files instead.
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)