Logic error in my code i think

Started by
16 comments, last by Randall Perkins 11 years, 2 months ago

This is the file


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>

    typedef struct {
        char map_title[68];
        int width,height;
        char map_tiles[100][100];
    } map_file;


    char *readFile(char *fileName) {
      FILE *file;
      char *code = malloc(1000 * sizeof(char));
      char *p = code;
      file = fopen(fileName, "r");
      do
      {
        *p++ = (char)fgetc(file);
      } while(*p != EOF);
      *p = '\0';
      return code;
    }

    map_file *load_map(char *location) {
        printf("Loading map : %s \n", location);

        map_file *tehMap;
        char *dataToken,*map_data;

        map_data = readFile(location);

            dataToken = strtok(map_data,",");

            printf("STARTED TOKEN OUTPUT\n");
            while(dataToken != NULL) {
                printf("TOKEN : %s \n",dataToken);
                dataToken = strtok(NULL,",");
            }
            printf("FINISHED TOKEN OUTPUT\n");


        return tehMap;
    }


int main()
{
    printf("Map engine testing ground for logic\n\n");

    map_file *theMap;
    theMap = load_map("map.txt"); // i plan on using the function to assign the values from the file to the struct

    return 0;
}

and ill attach map.map so you have something to test with, but my issue is in stdout only like half of the map file is output im not sure if it a char error of something in the strtok function.

any tips or help would be mean a lot, thank you for your time smile.png

Advertisement

First off, you have a nasty buffer overrun issue when your map reading code.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Your map.txt is 2KB large, and you try to read it into a 1000 byte big buffer

how would anyone recommend in using malloc when the size of the file will change depending on the map ???????

Either you check the size of the file and allocate that amount, or you process the file in parts if you can't (or don't want to) fit the whole file in memory.

i tryed this but i think the malloc function is making the program crash


    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;
    }

    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));
      code = malloc(sizeFile);
      char *p = code;

      do {
        *p++ = (char)fgetc(file);
      } while(*p != EOF);

      *p = '\0';
      return code;
    }
Nothing's jumping out at me apart from the fact that you don't test the return from fopen(). If you walk through it with the debugger can you see anything unusual (such as a negative 1 from fsize() getting passed to malloc())?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Nothing's jumping out at me apart from the fact that you don't test the return from fopen(). If you walk through it with the debugger can you see anything unusual (such as a negative 1 from fsize() getting passed to malloc())?

Khatharr it happens at the malloc function

i tested the fsize functions it returns 2374 for the file so that means the file is open and return a size

so after checking everything i have to be doing the malloc wrong ?

A call to malloc() should not result in an instant crash unless some corruption is happening somewhere. Does the program just immediately fail or does malloc() return NULL?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Khatharr it fails at the do { }

i tested it by doing this


    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;
    }

    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));
      code = malloc(sizeFile * sizeof(char));
      printf("Test for after malloc\n");
      char *p = code;
      printf("after sign pointer to code which is malloc");
      do {
        *p++ = (char)fgetc(file);
      } while(*p != EOF);
      printf("after the do function\n");
      *p = '\0';
      return code;
    }

the console gets to "after sign pointer to code which is malloc" and quits after that

This topic is closed to new replies.

Advertisement