Jump to content
  • Advertisement
Sign in to follow this  
Randall Perkins

Logic error in my code i think

This topic is 2149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by MrPhoenix

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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())? Edited by Khatharr

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!