Just a small problem with file handling

Okay so I am not so good at files, and needed some help from some of you guys

The thing is that i made a snake game for my project and I gotta complete it before 15th of September.

Everything is ready, EVERYTHING, except for the top three high score's storage.

I have no idea what to do and have got little knowledge about files. Please help me!

In the code below, score is the actual score the player makes.

I know you will find this code hard to understand but please try your best, or I'm doomed!!!

By the way, the tempStore function has been used for sorting the top three scores and then setting them in order.


I tried so hard making this and when I found out that it didn't work, even after so much of time and hard work, I felt like it was time i killed my self :(


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  class highScore
   {
       int scor;
       char name[50];
     public:
       void tempStore(int &score);
       void entername(int score)
	  {
	     setcolor(BLUE);
	     bar(130,100,530,380);
	     setcolor(RED);
	     settextstyle(0,0,0);
	     gotoxy(20,12);
	     cout<<"Score : ";
	     cout<<score;
	     gotoxy(20,14);
	     cout<<"Enter your name : ";
	     gets(name);
	   }
       void display(int scor)
	   {
	     char prtsco[100];
	     setcolor(BLUE);
	     bar(100,100,630,470);
	     setcolor(GREEN);
	     settextstyle(0,0,1);
	     outtextxy(100,100,name);
	     sprintf(prtsco,"%d",scor);
	     outtextxy(100,130,prtsco);
	   }
    }P;

void highScore::tempStore(int &score)
	  {
	     fstream hi;
	     hi.open("SCORE.dat",ios::app|ios::in|ios::binary|ios::out);
	     int temp[3],i=0,sc[3];
	     hi.read((char *)&P, sizeof(P));
	     while(!hi.eof())
		{
		    temp[i]=scor;
		    i++;
		    hi.read((char *)&P, sizeof(P));
		}
	     hi.close();
	     hi.open("SCORE.dat",ios::binary|ios::out);
	     while(!hi.eof())
		{
		    if(score>=temp[2])
			{
			   if(score>=temp[1])
				{
				    if(score>=temp[0])
					 {
					     sc[2]=score;
					     entername(score);
					 }
				     else
					 {
					     sc[1]=score;
					     entername(score);
					 }
				}
			   else
				{
				     sc[0]=score;
				     entername(score);
				}
			}
		    hi.write((char *)&P, sizeof(P));
		}
	     i=2;
	     hi.close();
	     hi.open("SCORE.dat",ios::in|ios::binary|ios::out);
	     while(!hi.eof())
		{
		    scor=sc[i];
		    i--;
		    hi.write((char *)&P, sizeof(P));
		}
	     hi.close();
	     hi.open("SCORE.dat",ios::in|ios::binary|ios::out);
	     hi.read((char *)&P, sizeof(P));
	     while(!hi.eof())
		{
		    display(scor);
		    hi.read((char *)&P, sizeof(P));
		}
	  }



Thanks :)
Topic archived. No new replies allowed.