Highscores problem

Ok so a friend of mine came up with this code to create a highscores list for a game when i run it, it works perfect you can see everything fine. the problem comes when it tries to reopen the file again. The file has abunch of gibberish inside and doesnt work any ideas?

ive played with it a little but cant seem to find the problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>

struct highscore {
char name[50];
int score;
};

main()
{
int i,j,tempscore,tempno;
char empty[]={"empty"};
ifstream fin("highscore.txt");
ofstream fout("highscore.txt");
highscore h[11];
j=0;
fin.seekg(0);
while (fin) {
fin.read((char*)&h[j],sizeof(highscore));
j++;
}
if (j==0) {
for (i=0;i<10;i++) {
strcpy(h[i].name,empty);
h[i].score=0;
}
}
fin.close();
cout<<"enter the namen";
cin.getline(h[10].name,50);
cout<<"enter the scoren";
cin>>h[10].score;
tempscore=0;
for (i=0;i<10;i++) {
tempscore=0;
for (j=i;j<11;j++) {
if (h[j].score>=tempscore) {
tempscore=h[j].score;
tempno=j;
}
}
char name[50];
int p;
p=h[i].score;
h[i].score=h[tempno].score;
h[tempno].score=p;
strcpy(name,h[i].name);
strcpy(h[i].name,h[tempno].name);
strcpy(h[tempno].name,name);

}
fout.seekp(0);
for (i=0;i<10;i++)
{
fout.write((char*)&h[i],sizeof(highscore));
cout<<h[i].name<<"tttt"<<h[i].score<<"n";
}
fout.close();
getch();
}

closed account (zwA4jE8b)
I notice that fout.write((char*)&h[i],sizeof(highscore)); is converting h[i] which is an array of stucts to a char*. But the struct contains an integer.

maybe use a different method instead of fout.write().....

fout << h[i].name << "|" << h[i].score;

then when you read in you can use getline() and delimit by '|' or whatever character you choose.
ok so i did the fout << h[i].name << "|" << h[i].score; and it compiled but the same thing was happening i wasn't sure what you meant about the getline() and delimiting.
closed account (zwA4jE8b)
#include <iomanip>

then you can use the getline() function.

getline(ostream, destination , 'd');


ostream = the input stream you are using, in this case 'fin';
destination = where you want the data to go, in this case h[i];
'd' = a character delimeter, whatever you want, can be '|'

1
2
3
4
5
6
for (i=0;i<10;i++)
{
getline(fin, h[i].name , '|');   //reads in the name
fin >> h[i].score;                  //reads in the score
fin >> ws;                            //reads past whitespace and other unuseable characters (newline) in the txt file.
}


something like that might work.

if you do it like this then your txt file should look like

name1|score1
name2|score2
name3|score3
etc...etc...


and the filewriter should be....

fout << h[i].name << "|" << h[i].score << endl;


sorry I couldnt help you debug the code you allready had.
Last edited on
no its fine ill be sure to try this method thanks a ton for your help!
Topic archived. No new replies allowed.