Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMrPhoenix

Posted 21 January 2013 - 05:58 AM

rip-off i changed the code around a lot but it freezes when i try to store the data from the char to the int tiles[1000] in the struct

 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
 
    typedef struct {
        char *map_title;
        int width,height,aot,tiles[1000];
    } map_file;
 
    static int fsize(FILE *fp){
        int prev=ftell(fp);
        fseek(fp, 0L, SEEK_END);
        int sz=ftell(fp);
        fseek(fp,prev,SEEK_SET); //go back to where we were
        return sz;
    }
 
 
    static char *readFile(char *fileName) {
      FILE *file;
      char *code;
      int sizeFile;
      file = fopen(fileName, "r");
      sizeFile = fsize(file);
      printf("Size of file : %i \n", sizeFile);
      code = malloc(sizeFile * sizeof(char));
      printf("Test for after malloc\n");
      fread(code, 1, sizeFile, file);
      fclose(file);
      return code;
    }
 
    // load a map file in to the struct
    map_file *load_map(char *location) {
        printf("Loading map : %s \n", location);
 
        map_file *tehMap;
        tehMap = malloc(sizeof(map_file*));
        char *dataToken,*map_data;
        int count = 1,tile_count = 1;
 
        map_data = readFile(location);
        dataToken = strtok(map_data,",");
            printf("STARTED TOKEN OUTPUT\n");
            while(dataToken != NULL) {
 
                if(count == 1) {
                    tehMap->map_title = dataToken;
                } else if(count == 2) {
                    tehMap->width = atoi(dataToken);
                }else if(count == 3) {
                    tehMap->height = atoi(dataToken);
                }else if(count > 3) {
                    tehMap->tiles[tile_count] = atoi(dataToken); // <------------- where it freezes
                    tile_count += 1;
                }
 
                printf("TOKEN : %s \n",dataToken);
                dataToken = strtok(NULL,",");
                count++;
            }
 
            tehMap->aot = tile_count + 1;
 
            printf("Amount of map inputs : %i \n", count);
            printf("FINISHED TOKEN OUTPUT\n\n");
            printf("tehMap struct -> Map title  : %s \n", tehMap->map_title);
            printf("tehMap struct -> Map width  : %i px \n", tehMap->width);
            printf("tehMap struct -> Map height : %i px \n", tehMap->height);
            printf("tehMap struct -> Map amount of tile    : %i \n", tehMap->aot);
 
 
        return tehMap;
    }
 
 
int main() {
    printf("Map engine testing ground for logic\n\n");
    map_file *theMap = NULL;
    theMap = load_map("map.map");
    return 0;
}

#2MrPhoenix

Posted 21 January 2013 - 05:57 AM

rip-off i changed the code around a lot but it freezes when i try to store the data from the char to the int tiles[1000] in the struct

 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
 
    typedef struct {
        char *map_title;
        int width,height,aot,tiles[1000];
    } map_file;
 
    static int fsize(FILE *fp){
        int prev=ftell(fp);
        fseek(fp, 0L, SEEK_END);
        int sz=ftell(fp);
        fseek(fp,prev,SEEK_SET); //go back to where we were
        return sz;
    }
 
 
    /* just something i thought that might set all the information in the struct
       and dont mind it
    static void init_map_file(map_file *input) {
        input->map_title = "";
        input->width = 0;
        input->height = 0;
        input->aot = 0;
        for(int s=0; s < 1000; s++) {
            input->tiles[s] = 0;
        }
    }
    */
 
    static char *readFile(char *fileName) {
      FILE *file;
      char *code;
      int sizeFile;
      file = fopen(fileName, "r");
      sizeFile = fsize(file);
      printf("Size of file : %i \n", sizeFile);
      code = malloc(sizeFile * sizeof(char));
      printf("Test for after malloc\n");
      fread(code, 1, sizeFile, file);
      fclose(file);
      return code;
    }
 
    // load a map file in to the struct
    map_file *load_map(char *location) {
        printf("Loading map : %s \n", location);
 
        map_file *tehMap;
        tehMap = malloc(sizeof(map_file*));
        char *dataToken,*map_data;
        int count = 1,tile_count = 1;
 
        map_data = readFile(location);
        dataToken = strtok(map_data,",");
            printf("STARTED TOKEN OUTPUT\n");
            while(dataToken != NULL) {
 
                if(count == 1) {
                    tehMap->map_title = dataToken;
                } else if(count == 2) {
                    tehMap->width = atoi(dataToken);
                }else if(count == 3) {
                    tehMap->height = atoi(dataToken);
                }else if(count > 3) {
                    tehMap->tiles[tile_count] = atoi(dataToken); // <------------- where it freezes
                    tile_count += 1;
                }
 
                printf("TOKEN : %s \n",dataToken);
                dataToken = strtok(NULL,",");
                count++;
            }
 
            tehMap->aot = tile_count + 1;
 
            printf("Amount of map inputs : %i \n", count);
            printf("FINISHED TOKEN OUTPUT\n\n");
            printf("tehMap struct -> Map title  : %s \n", tehMap->map_title);
            printf("tehMap struct -> Map width  : %i px \n", tehMap->width);
            printf("tehMap struct -> Map height : %i px \n", tehMap->height);
            printf("tehMap struct -> Map amount of tile    : %i \n", tehMap->aot);
 
 
        return tehMap;
    }
 
 
int main() {
    printf("Map engine testing ground for logic\n\n");
    map_file *theMap = NULL;
    theMap = load_map("map.map");
    return 0;
}

#1MrPhoenix

Posted 21 January 2013 - 05:56 AM

rip-off i changed the code around a lot


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
 
    typedef struct {
        char *map_title;
        int width,height,aot,tiles[1000];
    } map_file;
 
    static int fsize(FILE *fp){
        int prev=ftell(fp);
        fseek(fp, 0L, SEEK_END);
        int sz=ftell(fp);
        fseek(fp,prev,SEEK_SET); //go back to where we were
        return sz;
    }
 
 
    /* just something i thought that might set all the information in the struct
       and dont mind it
    static void init_map_file(map_file *input) {
        input->map_title = "";
        input->width = 0;
        input->height = 0;
        input->aot = 0;
        for(int s=0; s < 1000; s++) {
            input->tiles[s] = 0;
        }
    }
    */
 
    static char *readFile(char *fileName) {
      FILE *file;
      char *code;
      int sizeFile;
      file = fopen(fileName, "r");
      sizeFile = fsize(file);
      printf("Size of file : %i \n", sizeFile);
      code = malloc(sizeFile * sizeof(char));
      printf("Test for after malloc\n");
      fread(code, 1, sizeFile, file);
      fclose(file);
      return code;
    }
 
    // load a map file in to the struct
    map_file *load_map(char *location) {
        printf("Loading map : %s \n", location);
 
        map_file *tehMap;
        tehMap = malloc(sizeof(map_file*));
        char *dataToken,*map_data;
        int count = 1,tile_count = 1;
 
        map_data = readFile(location);
        dataToken = strtok(map_data,",");
            printf("STARTED TOKEN OUTPUT\n");
            while(dataToken != NULL) {
 
                if(count == 1) {
                    tehMap->map_title = dataToken;
                } else if(count == 2) {
                    tehMap->width = atoi(dataToken);
                }else if(count == 3) {
                    tehMap->height = atoi(dataToken);
                }else if(count > 3) {
                    tehMap->tiles[tile_count] = atoi(dataToken); // <------------- where it freezes
                    tile_count += 1;
                }
 
                printf("TOKEN : %s \n",dataToken);
                dataToken = strtok(NULL,",");
                count++;
            }
 
            tehMap->aot = tile_count + 1;
 
            printf("Amount of map inputs : %i \n", count);
            printf("FINISHED TOKEN OUTPUT\n\n");
            printf("tehMap struct -> Map title  : %s \n", tehMap->map_title);
            printf("tehMap struct -> Map width  : %i px \n", tehMap->width);
            printf("tehMap struct -> Map height : %i px \n", tehMap->height);
            printf("tehMap struct -> Map amount of tile    : %i \n", tehMap->aot);
 
 
        return tehMap;
    }
 
 
int main() {
    printf("Map engine testing ground for logic\n\n");
    map_file *theMap = NULL;
    theMap = load_map("map.map");
    return 0;
}

PARTNERS