forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

reading a file with highscores?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon May 22, 2006 7:00 pm    Post subject: reading a file with highscores? Reply with quote

Hi folks,

me again (just about ready to release the first version of my game so all the things i couldn't do or gave me errors a left must be handled now so that why the questions :S )

I have this read and save function of my highscores. the problem is however that it save the values and i pass the \n function to make a new line
how can read the value only to that point? i mean now it reads the file and shows the whole while including the strange character for a new line saved in the file but a new line is not shown.

any ideas on this?

EDIT NEW:

i have read the file and i use an for loop to loop through the entire char array to extract the pieces but how do i save the char from char[0] to char[5] into an integer? i now cast it but without much success
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Mon May 22, 2006 8:54 pm    Post subject: Reply with quote

To convert strings to integers use atoi() or, better still, strtol() or strtoul().
Use sprintf() to convert the integers back to strings.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon May 22, 2006 9:12 pm    Post subject: Reply with quote

thanks but if have another problem:

Code:
if (writepath[a] == '*'){
            if (aTmp == 1) {
               aCurrent += 1;
               sBuffer == NULL;
               for (i=aLast;i<a;i++){
                  sprintf(sBuffer, "%s%s",sBuffer,writepath[i]);
               }
               DebugOutput(sBuffer);
               iHigh[aCurrent] = atoi(sBuffer);
               //DebugOutput(sBuffer);
               //sTmp = sBuffer;
               aLast = a+1;
               aTmp += 1;
            }


the writepath is a char but my compiler says that i give an integer in and i use %s how can i add it to sbuffer ?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Mon May 22, 2006 10:06 pm    Post subject: Reply with quote

Ghoti, I wonder what piece of code should do :) I see a bit of Lua-ism there.

I think the easiest way to read/write simple records to a file is to use the fprintf/fscanf combo. Say, you want to write the name of the player and a score it could be pretty much like this:

Code:

#include <stdio.h>
#include <string.h>

struct Score
{
   char   name[64];
   int      score;
};

Score   g_scoreboard[100];
int      g_nScores = 0;

void AddScore(const char* name, int score)
{
   strncpy(g_scoreboard[g_nScores].name, name, 64);
   g_scoreboard[g_nScores].score = score;
   g_nScores++;
}

void ClearScoreboard()
{
   for(int i = 0; i < g_nScores; i++)
   {
      g_scoreboard[i].name[0] = '\0';
      g_scoreboard[i].score = 0;
   }
   g_nScores = 0;
}

void DumpScoreboard()
{
   printf("Scoreboard:\n");
   for(int i = 0; i < g_nScores; i++)
      printf(" - %s %d\n", g_scoreboard[i].name, g_scoreboard[i].score);
}

void WriteScoreboard()
{
   FILE*   fp = fopen("score.txt", "w");
   if(!fp)
      return;
   fprintf(fp, "%d\n", g_nScores);
   for(int i = 0; i < g_nScores; i++)
      fprintf(fp, "%s %d\n", g_scoreboard[i].name, g_scoreboard[i].score);
   fclose(fp);
}

void ReadScoreboard()
{
   FILE*   fp = fopen("score.txt", "r");
   if(!fp)
      return;
   fscanf(fp, "%d\n", &g_nScores);
   for(int i = 0; i < g_nScores; i++)
      fscanf(fp, "%s %d\n", g_scoreboard[i].name, &g_scoreboard[i].score);
   fclose(fp);
}

int main(int argc, char* argv[])
{
   AddScore("Mikko", 100);
   AddScore("Esko", 1230);
   AddScore("Petteri", 10);

   WriteScoreboard();
   DumpScoreboard();

   ClearScoreboard();
   DumpScoreboard();

   ReadScoreboard();
   DumpScoreboard();

   return 0;
}

You dont really need to write the amount of records, just use feof() instead... that above format just happens to be my preference, and more allocation friendly too :)

Another thing that starting programmers will miss is how to pass arguments to scanf. It wants pointers, so the reading the string is a bit missleading since it deals with an array.
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon May 22, 2006 11:14 pm    Post subject: Reply with quote

hmmm yeah well i'm almost done with my pieces of code and it works almost so i'll stick to my code but thanks for the code anyway

i still use the sceio opening tools and i have a question:

when i save now the data is put behind the data already there.

i've tried this:
Code:
i = sceIoRemove("highscore.txt");
      fdout = sceIoOpen("highscore.txt", PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
      

this doesn't work, it is not deleted and i also tried:
Code:
i = sceIoRemove("highscore.txt");
      fdout = sceIoOpen("highscore.txt", PSP_O_WRONLY | PSP_O_CREAT, 0777);


how can i erase the file (even delete cuase the psp_o_creat creates the file when it not exist i presume.) so that the data is not put at the end but everytime overwrites the existing data?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group