Very weird C programing bug

i have been working on this bug for ages in my level editor's load(); function
and cant seem to fix it infact the ony thing i managed to make it do is 1 read the executeble program!!!(itsself)
and
2. read from this weird C:\Windows\Sxs directory

Heres the function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void load()//loads program not working...
{
 short i;
 short j;
 short k;
 char ch[255];
 //char File[9] = "Map.TTXT";
 Clear_Screen();
 printf("loading...\n");
 char File[9] = "Map.TTXT";
 FILE *fp;
 fp=fopen(File, "r");
 for (i = 0;i < MAXX;i++){

      for(j = 0;j < MAXY;j++){
            fgets(ch, ch[k], fp);
            Set_Map(i, j, ch[k]);
            k++;
               }
            }
      fclose(fp);
 printf("\nfinished\n");
 getch();
 Set_walls();
}
You seem to be misusing fgets().

http://www.cplusplus.com/reference/cstdio/fgets/

char * fgets ( char * str, int num, FILE * stream );

The second parameter, num, is supposed to be:

num
    Maximum number of characters to be copied into str (including the terminating null-character).


16
17
fgets(ch, sizeof ch, fp); // some people also do:
fgets(ch, 255, fp); // hard-coded value of 255, not recommended 


That said, there are plenty other places for your load() function to fail.
Maybe Set_Map() fails. Maybe Set_walls() fails. You must check them all for bugs.
i had the 2nd parameter of fgets set to the size of my ch[] array before but it did not work

i can print out ch[] and it shows the contents of the file fine its just setting it to
the Map.Map[][] 2d array that i cant do for some reason i even tried directly assigning it and i failed!!!

here's my setmap function:
1
2
3
4
5
signed short Set_Map(signed short x, signed short y, signed short i)
{
 Map.Map[x][y] = i;
 return i;
}


here is the map struct its refering to:

1
2
3
4
5
struct
{
 unsigned char ID[1500];//not currently used
 unsigned char Map[MAXX][MAXY];//used to store the level
} Map;


and setwalls is just a bunch of Set_Map functions

so how could this happen i must of doubble checked my code 4 times now!!! (8 times!!!)
Last edited on
Well I see that you are incrementing k without having initialized it.
Variables which are not initialized (or assigned a value to) before their value is used are said to hold "garbage".
In your case, k is not guaranteed to hold 0 (you rely on this for k++ to work correctly).

1
2
3
4
5
6
7
void load()
{
 short i;
 short j;
 short k = 0; // initialize it

 // ... 
i have got rid of k and this is my function now


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void load()//loads program not working...
{
 short i;
 short j;
 short k = 0;
 unsigned char ch[1500];
 //char File[9] = "Map.TTXT";
 Clear_Screen();
 printf("loading...\n");
 char File[9] = "Map.TTXT";
 FILE *fp;
 fp=fopen(File, "r");
 for (i = 0;i < MAXX;i++){

      for(j = 0;j < MAXY;j++){
            fgets(ch, 1500, fp);
            Set_Map(i, j, ch[0]);
            k++;
               }
            }
      fclose(fp);
 printf("\nfinished\n");
 getch();
 Set_walls();
}


it loads the file but it stores it as 1 2 and 5 etc not the ASCII characters it should

dose anyone know why this would happen???
Last edited on
Topic archived. No new replies allowed.