Array trouble

Non-platform specific questions.

Array trouble

Postby SuperSonic » Wed Oct 12, 2011 7:44 pm

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
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Array trouble

Postby skydereign » Wed Oct 12, 2011 8:39 pm

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?
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Array trouble

Postby SuperSonic » Wed Oct 12, 2011 9:35 pm

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?
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Array trouble

Postby skydereign » Wed Oct 12, 2011 9:49 pm

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]);
        }
    }
  }
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Array trouble

Postby SuperSonic » Wed Oct 12, 2011 10:08 pm

Thank you Sky. I understand this ok :D

+1 :wink:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Array trouble

Postby EvanBlack » Thu Oct 13, 2011 8:01 pm

Hey thanks! I needed to know more about this same subject too XD
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest