reading a file with highscores?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

reading a file with highscores?

Post by Ghoti »

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
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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

Jim
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

thanks but if have another problem:

Code: Select all

if (writepath[a] == '*'){
				if (aTmp == 1) {
					aCurrent += 1;
					sBuffer == NULL;
					for &#40;i=aLast;i<a;i++&#41;&#123;
						sprintf&#40;sBuffer, "%s%s",sBuffer,writepath&#91;i&#93;&#41;;
					&#125;
					DebugOutput&#40;sBuffer&#41;;
					iHigh&#91;aCurrent&#93; = atoi&#40;sBuffer&#41;;
					//DebugOutput&#40;sBuffer&#41;;
					//sTmp = sBuffer;
					aLast = a+1;
					aTmp += 1;
				&#125;
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 ?
memon
Posts: 63
Joined: Mon Oct 03, 2005 10:51 pm

Post by memon »

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: Select all

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

struct Score
&#123;
	char	name&#91;64&#93;;
	int		score;
&#125;;

Score	g_scoreboard&#91;100&#93;;
int		g_nScores = 0;

void AddScore&#40;const char* name, int score&#41;
&#123;
	strncpy&#40;g_scoreboard&#91;g_nScores&#93;.name, name, 64&#41;;
	g_scoreboard&#91;g_nScores&#93;.score = score;
	g_nScores++;
&#125;

void ClearScoreboard&#40;&#41;
&#123;
	for&#40;int i = 0; i < g_nScores; i++&#41;
	&#123;
		g_scoreboard&#91;i&#93;.name&#91;0&#93; = '\0';
		g_scoreboard&#91;i&#93;.score = 0;
	&#125;
	g_nScores = 0;
&#125;

void DumpScoreboard&#40;&#41;
&#123;
	printf&#40;"Scoreboard&#58;\n"&#41;;
	for&#40;int i = 0; i < g_nScores; i++&#41;
		printf&#40;" - %s %d\n", g_scoreboard&#91;i&#93;.name, g_scoreboard&#91;i&#93;.score&#41;;
&#125;

void WriteScoreboard&#40;&#41;
&#123;
	FILE*	fp = fopen&#40;"score.txt", "w"&#41;;
	if&#40;!fp&#41;
		return;
	fprintf&#40;fp, "%d\n", g_nScores&#41;;
	for&#40;int i = 0; i < g_nScores; i++&#41;
		fprintf&#40;fp, "%s %d\n", g_scoreboard&#91;i&#93;.name, g_scoreboard&#91;i&#93;.score&#41;;
	fclose&#40;fp&#41;;
&#125;

void ReadScoreboard&#40;&#41;
&#123;
	FILE*	fp = fopen&#40;"score.txt", "r"&#41;;
	if&#40;!fp&#41;
		return;
	fscanf&#40;fp, "%d\n", &g_nScores&#41;;
	for&#40;int i = 0; i < g_nScores; i++&#41;
		fscanf&#40;fp, "%s %d\n", g_scoreboard&#91;i&#93;.name, &g_scoreboard&#91;i&#93;.score&#41;;
	fclose&#40;fp&#41;;
&#125;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	AddScore&#40;"Mikko", 100&#41;;
	AddScore&#40;"Esko", 1230&#41;;
	AddScore&#40;"Petteri", 10&#41;;

	WriteScoreboard&#40;&#41;;
	DumpScoreboard&#40;&#41;;

	ClearScoreboard&#40;&#41;;
	DumpScoreboard&#40;&#41;;

	ReadScoreboard&#40;&#41;;
	DumpScoreboard&#40;&#41;;

	return 0;
&#125;
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.
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

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: Select all

i = sceIoRemove&#40;"highscore.txt"&#41;;
		fdout = sceIoOpen&#40;"highscore.txt", PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777&#41;;
		
this doesn't work, it is not deleted and i also tried:

Code: Select all

i = sceIoRemove&#40;"highscore.txt"&#41;;
		fdout = sceIoOpen&#40;"highscore.txt", PSP_O_WRONLY | PSP_O_CREAT, 0777&#41;;
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?
Post Reply