Saving map to binary file.

This is a discussion on Saving map to binary file. within the Game Programming forums, part of the General Programming Boards category; Hello, As a projekt in my school we are supposed to create a game, and my job was to make ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Saving map to binary file.

    Hello,
    As a projekt in my school we are supposed to create a game, and my job was to make a map maker.
    Ive got the mapmaker working, but i cant seem to save the map into a binary file.

    We are using SDL,OpenGl and C.
    The map is divided in a 30*30 "tile field" and my idea i to write all tiles to a binary like this:
    Code:
                        utfil=fopen("test.dat","wb");
                          for(i=0;i<MAP_WIDTH;i++)
                            for(u=0;u<MAP_HEIGHT;u++)
                         {
                            fwrite(&map[i][u].background,sizeof(int),1,infil);
                            fwrite(&map[i][u].walkable,sizeof(int),1,infil);
                            fwrite(&map[i][u].decoration,sizeof(int),1,infil);
                          }
    And then load it in the same way, i just chnage fwrite to fread like so:
    Code:
                        infil=fopen("test.dat","rb");
                          for(i=0;i<MAP_WIDTH;i++)
                            for(u=0;u<MAP_HEIGHT;u++)
                          {
                            fread(&map[i][u].background,sizeof(int),1,infil);
                            fread(&map[i][u].walkable,sizeof(int),1,infil);
                            fread(&map[i][u].decoration,sizeof(int),1,infil);
                           }
                        fclose(infil);
    The struct im using for the map looks líke this:
    Code:
    struct Tile
    {
        int background;
        int walkable;
        int decoration;
    };
    I've been stuck at this for maaany hours now, and im geeting tired of it. Been googling alot, but found nothing that worked.
    Does any of your brilliant minds have a solution?

    If u need more of my code please let me know.

    Best Regards

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    You use the variable utfile in your code snippet for writing, but you fwrite to infil. Is this a copy/paste error in your post, or perhaps the source of your problem?

    If it is a copy/paste error and your actual code uses the correct variable names, then can you be a little more specific about what is not working? Is no file showing up named test.dat? Are you getting a compile error? Seg-fault when trying to load from the file? etc.
    C+/- programmer extraordinaire

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    That is indeed just a copy/paste error, i write to utfil and read to infil.

    It does create a file, but it seems only to be filled with blanks(spaces), so I cant anything from it.

    Edit: I've managed to solve the problem
    Ty for your help
    Last edited by Qrypt; 04-14-2011 at 05:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving Struct to Binary File/Copying Structs
    By timmeh in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2009, 09:44 PM
  2. Saving linked lists to a binary file
    By kotoko in forum C Programming
    Replies: 1
    Last Post: 06-14-2008, 07:41 PM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  4. Saving in binary
    By bigdan43 in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2005, 04:23 PM
  5. saving a binary tree??
    By fayte in forum C++ Programming
    Replies: 9
    Last Post: 01-27-2005, 01:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21