Page 1 of 1

Array trouble

PostPosted: Wed Oct 12, 2011 7:44 pm
by SuperSonic
In my global code I have this:
Code: Select all
FILE* LEVEL = fopen("Level1.lev", "r");
int World[10][10] = {LEVEL};

When I try to run this, I get an error. Can anyone help me? :D

Re: Array trouble

PostPosted: Wed Oct 12, 2011 8:39 pm
by skydereign
That's not going to work, because a FILE* isn't just the text in the file. You have to read in the file using fscanf, or similar. Though chances are its going to be pretty simple just using two for loops. But, if you need help, then what is the format of the Level1.lev file?

Re: Array trouble

PostPosted: Wed Oct 12, 2011 9:35 pm
by SuperSonic
level1.lev is just a test file that looks like this:
Code: Select all
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 2, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1

1 is a wall, 2 is the spawn point for the player, and 0 is empty space. Does that answer your question?

Re: Array trouble

PostPosted: Wed Oct 12, 2011 9:49 pm
by skydereign
Ok, well this should do it. Do ask if you don't fully understand what it is doing.
Code: Select all
map_read (char* file_name, int world[][10])
{
  FILE* file = fopen(file_name, "r");
  int i, j;
  if(file!=NULL)
  {
    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {
            fscanf(file, "%d, ", &world[i][j]);
        }
    }
  }
}

Re: Array trouble

PostPosted: Wed Oct 12, 2011 10:08 pm
by SuperSonic
Thank you Sky. I understand this ok :D

+1 :wink:

Re: Array trouble

PostPosted: Thu Oct 13, 2011 8:01 pm
by EvanBlack
Hey thanks! I needed to know more about this same subject too XD