getting map from a file

This is a discussion on getting map from a file within the C Programming forums, part of the General Programming Boards category; I'm trying to make a function to get a map from a file for a rpg. The header values are ...

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    getting map from a file

    I'm trying to make a function to get a map from a file for a rpg. The header values are coming in fine, but as soon as I try to read the body of the file I get garbage values.
    I'm using this method:
    Code:
    map_t::map_t(char* base) {
      char filename[255];
      int i, j;
      tiles = new tiles_t(base);
      sprintf(filename,"maps/%s/%s.map",base,base);
      FILE *fp = fopen(filename,"r");
      if (fp == NULL) {
        allegro_message("Unable to open file \"%s\"",filename);
        exit(1);
      }
      fscanf(fp,"%d %d %d %d",&mapw,&maph,&screenw,&screenh);
      tilew = SCREEN_W / screenw;
      tileh = SCREEN_H / screenh;
      map = new int*[mapw];
      for (i = 0; i < mapw; ++i)
        map[i] = new int[maph];
      for (j = 0; j < maph; ++j)
        for (i = 0; i < mapw; ++i) {
          scanf("%d",&(map[i][j]));
          if (map[i][j] < 0 || map[i][j] >= tiles->numtiles) {
            allegro_message("Invalid tile value %d at (%d,%d) in file %s",
              map[i][j],i,j,filename);
            exit(1);
          }
        }
      fclose(fp);
    }
    and this data:
    Code:
    10 10 5 5
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0
    It's a total mystery to me why fscanf would get some of the values and not others. Is there some C newline trick that I'm missing or what?
    Illusion and reality become impartiality and confidence.

  2. #2
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Scanf? Boy, that just makes me feel stupid.
    Illusion and reality become impartiality and confidence.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You should avoid posting C++ code to a C board.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You are probably making the issue a lot harder by not using the tools needed to get the job done. There are several array-based file i/o functions available in C. A simple block read from the disk into a 1D array will do the trick. Then in game access the map as y*width_of_map+x or calc it once and then keep a running tab of the player's position.

    Then a simple is:

    north: player.mappos-=map_width;
    south: player.mappos+=map_width;
    east: player.mapppos++;
    west: player.mappos--;

    Look into the _open (open), _read (read) functions.

    Or you can always read the data into an array and copy it later into one of the STL container's. I personally would not do this, but it's your game.

  5. #5
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Quote Originally Posted by itsme86
    You should avoid posting C++ code to a C board.
    I know I know. I wasn't really sure to post this since it's for a game with allegro, uses c++ structuring, and has a problem with c functions. I figured the problem was why I was posting, so I'd choose the board according to that.
    Illusion and reality become impartiality and confidence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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