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.

C/C++ > Needing help with IO

#156750 - darkchild - Tue May 13, 2008 6:31 pm

Hey, I just released a game called memorizeME

Since lots of people enjoyed it.. I've been wanting to make a highscore function...but I ran into a bit of trouble, whenever I try to write something to the file, it crashes, but reading from it works!

What am I doing wrong here?

Code:


int hiscore=0;
void startio(void)
{
   PA_InitText(1,3);
   PA_OutputText(1,0,0, "Starting FAT...");
   fatInitDefault();

   PA_OutputText(1,0,1, "Reading HighScore Data..");
   FILE* testRead = fopen ("/memorizeme.dat", "rb"); //rb = read
   if (testRead)
   {
      char buffer[30];
      fread(buffer, 30, 1, testRead);
      
      hiscore = atoi(buffer);
   }
   else
   {
      fclose(testRead);
      PA_OutputText(1,0,2,"Not present! Now creating...");
      FILE* testWrite = fopen ("/memorizeme.dat", "wb"); //wb = create/truncate & write
      fwrite("0", 1, 1, testWrite);
      fclose(testWrite);
   }
   PA_DualResetBg();
}

void updatehighscore(void)
{

   FILE* testwrite = fopen ("memorizeme.dat", "wb"); //wb = create/truncate & write
   char buffer[10];
   int n;
   n=sprintf (buffer, "%d", hiscore);
   fwrite(&buffer, 10, 1, testwrite);
   fclose(testwrite);

}

#156752 - simonjhall - Tue May 13, 2008 6:55 pm

I think the "fclose(testRead)" in the else statement could the line that's causing you grief. If the file opening process failed you won't be returned a valid handle...and you then try to close that. I'd guess that's what's nuking it.

Just a guess ;-)
_________________
Big thanks to everyone who donated for Quake2

#156758 - darkchild - Tue May 13, 2008 7:52 pm

I don't think that's it...

it only crashes when you try to write to the file =/

#156759 - Dwedit - Tue May 13, 2008 7:53 pm

your paths don't match
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#156771 - darkchild - Tue May 13, 2008 9:40 pm

That's because I was trying to vary, thought it was the problem, but with the "\" it still crashes =/

#156777 - simonjhall - Tue May 13, 2008 10:36 pm

Which is the line that's crashing it then?
_________________
Big thanks to everyone who donated for Quake2

#156808 - darkchild - Wed May 14, 2008 1:14 am

the writing one xD (Fwrite)

#156820 - simonjhall - Wed May 14, 2008 7:41 am

Which one? ;-)
In the first one (in the else statement) can you stick in a test to ensure that the fopen worked?
In the second one, shouldn't it be fwrite(buffer rather than fwrite(&buffer? buffer is already a pointer - you shouldn't need to dereference it again.
_________________
Big thanks to everyone who donated for Quake2

#156823 - Normmatt - Wed May 14, 2008 8:00 am

why ain't you using fprintf, if you trying to write a string to a file??

#156841 - darkchild - Wed May 14, 2008 5:58 pm

I am now..

and I changed the code,

here's how it is now:

Code:

int hiscore=0;
bool fat=true;
void startio(void)
{
   PA_InitText(1,3);
   PA_OutputText(1,0,0, "Starting FAT...");
   if (fatInitDefault() == false)
   {
      PA_OutputText(1,0,1, "FAT ERROR!");
      PA_WaitFor(Stylus.Newpress || Pad.Held.A || Pad.Held.B || Pad.Held.X || Pad.Held.Y || Pad.Held.Up || Pad.Held.Down || Pad.Held.Left || Pad.Held.Right);
      fat = false;
   }
   if (fat)
   {
      PA_OutputText(1,0,1, "Reading HighScore Data..");
      FILE* testRead = fopen ("/memorizeme.dat", "rb"); //rb = read
      if (testRead)
      {
         char buffer[30];
         fread(buffer, 30, 1, testRead);
         fclose(testRead);
         hiscore = atoi(buffer);
      }
      else
      {
         PA_OutputText(1,0,2,"Not present! Now creating...");
         FILE* testWrite = fopen ("/memorizeme.dat", "wb"); //wb = create/truncate & write
         fwrite("0", 1, 1, testWrite);
         fclose(testWrite);
      }
      PA_DualResetBg();
   }
}

void updatehighscore(void)
{
   if (fat)
   {
      FILE* testwrite = fopen ("/memorizeme.dat", "wb"); //wb = create/truncate
      fprintf(testwrite, "%d", hiscore);/*
      char buffer[10];
      int n;
      n=sprintf (buffer, "%d", hiscore);
      fwrite(&buffer, strlen(buffer), sizeof(buffer), testwrite);*/
      fclose(testwrite);
   }

}


and by the looks of it, he now crashes at this line: (in the updatehighscore function)

Code:

      FILE* testwrite = fopen ("/memorizeme.dat", "wb"); //wb = create/truncate

#156855 - Dwedit - Wed May 14, 2008 9:04 pm

out of curiosity, is it fat16 and are there lots of files in the root directory?
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#156867 - simonjhall - Wed May 14, 2008 11:07 pm

Can you ensure that there's absolutely nothing else breaking the code, by just sticking in the fopen that you think breaks it in a program with nothing else but the bare minimum?
_________________
Big thanks to everyone who donated for Quake2

#156869 - darkchild - Wed May 14, 2008 11:31 pm

It's FAT32....
and OMG! I put the code in main.c before anything else started, and it wrote! :|

What could be making this? =/

#156873 - silent_code - Thu May 15, 2008 12:34 am

i get this type of behaviour all the time when working with win32 windows... it's so annoying! the right order of things is *everything* in those cases.
i hope you find a solution very soon. :^)

happy coding!
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#156883 - darkchild - Thu May 15, 2008 1:55 am

then what is your advice?

Should he write as soon as a new highscore is detected BEFORE showing all the GFX?

(Would these things stop if I were using linux?)

#156885 - yellowstar - Thu May 15, 2008 4:25 am

darkchild wrote:

Code:

void startio(void)
{
   if (fat)
   {
      PA_OutputText(1,0,1, "Reading HighScore Data..");
      FILE* testRead = fopen ("/memorizeme.dat", "rb"); //rb = read
      if (testRead)
      {
         char buffer[30];
         fread(buffer, 30, 1, testRead);
         fclose(testRead);
         hiscore = atoi(buffer);
      }
      else
      {
         PA_OutputText(1,0,2,"Not present! Now creating...");
         FILE* testWrite = fopen ("/memorizeme.dat", "wb"); //wb = create/truncate & write
         fwrite("0", 1, 1, testWrite);
         fclose(testWrite);
      }
   }
}

void updatehighscore(void)
{
   if (fat)
   {
      FILE* testwrite = fopen ("/memorizeme.dat", "wb"); //wb = create/truncate
      fprintf(testwrite, "%d", hiscore);/*
      char buffer[10];
      int n;
      n=sprintf (buffer, "%d", hiscore);
      fwrite(&buffer, strlen(buffer), sizeof(buffer), testwrite);*/
      fclose(testwrite);
   }

}


and by the looks of it, he now crashes at this line: (in the updatehighscore function)

Code:

      FILE* testwrite = fopen ("/memorizeme.dat", "wb"); //wb = create/truncate



That test string you're trying to write, it's actually 2 bytes long, including the terminating null character. wb/rb is only for binary, you're trying to use text with it. Switch it to just w. And you should check if opening succeeded - I use if(f==NULL)return;(Switch the f to testwrite in your case)

If that didn't fix it, what exactly is happening? Program hangs, or Red screens of Death?(Red screen with white error message on top)
Does the file even get created?

Quote:

(Would these things stop if I were using linux?)

No. silent_code was referring to programming programs for PC, not NDS.

#156901 - simonjhall - Thu May 15, 2008 5:24 pm

Last time I checked, libfat didn't support text mode reading/writing so internally it all defaults to rb/wb.
And you really need to pull apart the program into the bare minimum in order to make it crash. Until then you can only guess what line is breaking, as even though a given line may not crash it may break some other part of the program causing the apparent crash at that line.
_________________
Big thanks to everyone who donated for Quake2

#156902 - darkchild - Thu May 15, 2008 5:52 pm

The file got created (when I put in the beginning of main.c for him to write)
but whenever I try it somewhere else... crashes! (or hangs, whatever term you prefer)

@Simonjhall:
I made a PA_OutputText for each line in my IO.C (where the IO code is at) and he doesn't get passed the opening one!