Hello folks!
I'm creating a wonderful word game and I want to create a highscore table too. I'm writing a highscore table with commands fread and fwrite but I'm getting a lot of errors. Please, give me some ideas how to write it.
Thx in advance.
Hello folks!
I'm creating a wonderful word game and I want to create a highscore table too. I'm writing a highscore table with commands fread and fwrite but I'm getting a lot of errors. Please, give me some ideas how to write it.
Thx in advance.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
how about reading all the stuff into an array, then use a bubble sort(or as such) to sort it out ?
how much work have you done on it ?
I got this so far:
Code:struct Table { char Player[80]; float Points; }; void Top10(int Category, float Points, char Player[]) { FILE *fin; char file1[100], *file1ptr, TempPlayer[80]; struct Table s[10]; int i=0; float TempPoints, Temp; file1ptr = file1; if (Category == 1) { file1ptr = "./DATA/POINTS/hil.dat"; } else if (Category == 2) { file1ptr = "./DATA/POINTS/his.dat"; } else if (Category == 3) { file1ptr = "./DATA/POINTS/hit.dat"; } TempPoints = Points; strncpy(TempPlayer, Player, 79); fin = fopen(file1ptr, "wb"); for (i=0; i<1; i++) { fread(&s[i], sizeof(struct Table), 1, fin); if (TempPoints <= s[i].Points) { Temp = TempPoints; s[i].Points = Temp; TempPoints = s[i].Points; } fwrite(&s[i], sizeof(struct Table), 1, fin); } fclose(fin); }
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
i havent looked at the actual code,
have you initilised all the variabled in the file ?
(do you have a file), if not maybe you need to create one,
think about it.
(im too tired sorry)
Just look at the code, please.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
do you know your for loop goes around once ?
Yes. This is beacuse the function is in the testing phase. The main problem is that when function write's into the file there are no values saved.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
nah sorry m8, i cant help u.
i havent done that much with file accessing.
i dont see any sorting, would you put them into an array,
then sort them there ?
fin = fopen(file1ptr, "wb");
"w" create text file for writing; discard previous contents if any
I suggest something like the following...fclose (fin);Code://Do the choice stuff up here.... // ... // Read the file into an array. fin = fopen (file1pte, "rb"); fread(&s[0], sizeof(struct Table), 10, fin); fclose (fin); for (i = 0; i < 10; i++) { // Place the new high score into the array. } // Write the array into the file. fin = fopen (file1ptr, "wb"); fwrite (&s[0], sizeof (struct Table), 10, fin); return; }
Callou collei we'll code the way
Of prime numbers and pings!
I've written so far:
This is working, but I have one error . This error is generated in the reseting section. Please take a look at the code .Code:struct Table { char Player[80]; float Points; }; void Top10(int Category, float Points, char Player[]) { FILE *fin; char file1[100], *file1ptr; struct Table s[10]; int i=0; float TempPoints, Temp; file1ptr = file1; if (Category == 1) { file1ptr = "./DATA/TOCKE/hil.dat"; } else if (Category == 2) { file1ptr = "./DATA/TOCKE/his.dat"; } else if (Category == 3) { file1ptr = "./DATA/TOCKE/hit.dat"; } TempPoints = Points; // reseting highscore table fin = fopen(file1ptr, "wb"); fread(&s[0], sizeof(struct Table), 10, fin); for (i=0; i<10; i++) { s[i].Points = 0.0; strncpy(s[i].Player, 0, 79); // if I have this here, than the error is generated. WHY? } fwrite (&s[0], sizeof (struct Table), 10, fin); fclose(fin); // sorting highscore table fin = fopen (file1ptr, "rb"); fread(&s[0], sizeof(struct Table), 10, fin); for (i = 0; i < 10; i++) { if (TempPoints > s[i].Points) { Temp = TempPoints; TempPoints = s[i].Points; s[i].Points = Temp; strncpy(s[i].Player, Player, 79); } } fin = fopen (file1ptr, "wb"); fwrite (&s[0], sizeof (struct Table), 10, fin); fclose(fin); // print out of highscore table printf("\n"); for (i=0; i<10; i++) { printf("\n%02d. %1.2f\t\t%s", i+1, s[i].Points, s[i].Player); }
Last edited by GaPe; 01-30-2002 at 10:52 AM.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
I just discovered this. If I write this "strncpy(s[i].Player, "no name", 79);" instead of this "strncpy(s[i].Player, 0, 79);" then the code is working properly. Why? What is wrong with the second sentence?
And another problem is bugging me.
I want that this highscore table will print out the name of the player too, but with this code I do not get the right results.
Please help.Code:// sorting highscore table fin = fopen (file1ptr, "rb"); fread(&s[0], sizeof(struct Table), 10, fin); for (i = 0; i < 10; i++) { if (TempPoints > s[i].Points) { Temp = TempPoints; TempPoints = s[i].Points; s[i].Points = Temp; if (s[i].Points == Points) strncpy(si].Player, Player, 79); } else strncpy(s[i].Player, s[i].Player, 79); } fin = fopen (file1ptr, "wb"); fwrite (&s[0], sizeof (struct Table), 10, fin); fclose(fin); // print out of the highscore table printf("\n"); for (i=0; i<10; i++) { printf("\n%02d. %-20s\t%1.2f", i+1, s[i].Player, s[i].Points); }
Last edited by GaPe; 01-30-2002 at 12:53 PM.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Folks! I really don't know what is wrong with my code and I need your help. Thx in advance.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Ok, I figured it out. The problem was that I didn't save the previous name of the player into other variable.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)