gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > efs problem....

#150184 - iprice - Wed Jan 30, 2008 11:11 pm

I have a text file containing:

mons 0 5 0 0 -600 3000 0 0
mons 1 1 1 31 600 6000 0 270
mons 1 1 1 31 0 8000 0 270
wait
turn 45
done

I want to read in the info and am using the following code:

Code:
void get_next(void)
{
char inst[4];
int ii=0;

EFS_fread(&inst, 1, sizeof(char)*4, file);
while (inst[0] == 'm')
{
   EFS_fread(&monsters[ii].type, 1, sizeof(int), file);
   EFS_fread(&monsters[ii].health, 1, sizeof(int), file);
   EFS_fread(&monsters[ii].action, 1, sizeof(int), file);
   EFS_fread(&monsters[ii].frame, 1, sizeof(int), file);
   EFS_fread(&monsters[ii].x, 1, sizeof(int), file);
   EFS_fread(&monsters[ii].y, 1, sizeof(int), file);
   EFS_fread(&monsters[ii].z, 1, sizeof(int), file);
   EFS_fread(&monsters[ii].rot, 1, sizeof(int), file);
   EFS_fread(&inst, 1, sizeof(char)*4, file);
ii++;
}
if (inst[0] == 'd')
{
   EFS_fclose(file);
   game_over = 1;
   return;
}
else if (inst[0] == 'w')
{
   player.waiting_for_monsters = 1;
   return;
}
else if (inst[0] == 't')
{
   EFS_fread(&player.turn, 1, sizeof(int), file);
}
else if (inst[0] == 'a')
{
   EFS_fread(&player.amount, 1, sizeof(float), file);
}
}


I know I can make it neater but hopefully you will get the idea.... is it the newline? the size of char string? any ideas?

Thanks

#150192 - nce - Thu Jan 31, 2008 1:29 am

hi,

you said "TEXT file" right ?

so reading :
"6000" as a sizeof(int) is wrong. it will read the ascii code for that ;)

you have to read : 4*sizeof(char) in a table
and convert this table with something like atoi; that function convert a string in a real integer :)

parsing a text file is a little harder that parsing a hexadecimal file ;)
_________________
-jerome-

#150223 - iprice - Thu Jan 31, 2008 5:24 pm

I now have:

Code:
int my_atoi(char string[])
{
   int value = 0;
   int sign = 1;
   int string_pnt = 0;

   if (string[string_pnt] == '-')
   {
      sign = -1;
      string_pnt++;
   }

   while (string[string_pnt])
   {
      if ((string[string_pnt] >= '0') && (string[string_pnt] <= '9'))
         value = value * 10 + (string[string_pnt] - '0');
      else
         break;
      string_pnt++;
   }
   return (sign * value);
}

void get_next_update(void)
{
   char inst[8];
   int ii;

   EFS_fread(&inst, 1, sizeof(char)*5, file);
   while (inst[0] == 'm')
   {
      for (ii = 0; ii < max_monsters; ii++)
         if (monsters[ii].health == 0)
            break;

      for(aa=0;aa<8;aa++)inst[aa]=' ';
      EFS_fread(&inst, 1, sizeof(char)*2, file);
      monsters[ii].type = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*2, file);
      monsters[ii].health = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*2, file);
      monsters[ii].action = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*2, file);
      monsters[ii].frame = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*5, file);
      monsters[ii].x = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*5, file);
      monsters[ii].y = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*5, file);
      monsters[ii].z = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*5, file);
      monsters[ii].rot = my_atoi(inst);
      EFS_fread(&inst, 1, sizeof(char)*5, file);
   }
   if (inst[0] == 'd')
   {
      EFS_fclose(file);
      game_over = 1;
      return;
   }
   else if (inst[0] == 'w')
   {
      player.waiting_for_monsters = 1;
      return;
   }
   else if (inst[0] == 't')
   {
      EFS_fread(&inst, 1, sizeof(char)*2, file);
      player.turn = my_atoi(inst);
   }
   else if (inst[0] == 'a')
   {
      EFS_fread(&inst, 1, sizeof(char)*2, file);
      player.amount = (float)my_atoi(inst);
   }
}


but it still isn't quite right..... my debug isn't working so I can't display the value in the string or varible to screen :(

#150231 - josath - Thu Jan 31, 2008 8:34 pm

1. get your debug print working first.
2. why are you using my_atoi() instead of the built-in atoi()?

#150234 - iprice - Thu Jan 31, 2008 9:19 pm

I have got it working.... I needed to allow for spaces in the string and for return characters. I used my own atoi so I knew what it was doing....

I have now got extern scripting working so my 3d rail shooter is looking good. I'll hopefully release a test version later this week......

:)

happy now....