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
reading a file with highscores?
reading a file with highscores?
My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
thanks but if have another problem:
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 ?
Code: Select all
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;
}
My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
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:
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.
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: Select all
#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;
}
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.
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:
this doesn't work, it is not deleted and i also tried:
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?
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: Select all
i = sceIoRemove("highscore.txt");
fdout = sceIoOpen("highscore.txt", PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
Code: Select all
i = sceIoRemove("highscore.txt");
fdout = sceIoOpen("highscore.txt", PSP_O_WRONLY | PSP_O_CREAT, 0777);
My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php