Data file handling!

I am making project(game) in c++. i want to store score and display in such a way that the score will be displayed of 5 players who had high score. if new player makes high score then the player with lower score is deleted and new player with high score is edited.

so i want little idea how do i do it. i know how to insert and delete record in data file handling but i dont know how do i merge it..

and creating file in textmode is better or binary mode is better.

Thank you.
Text mode is easier and can be read by any program, binary mode is smaller and is harder to "fudge" if someone is trying to manipulate your data.

If you're using text mode, I'd do something like this:
1
2
3
4
5
struct MyStruct{
  char[20] player;
  int score;
  bool operator() (MyStruct i,MyStruct j) { return (i.score<j.score); }
};


Then you make an array of myStruct and feed it into the std::sort algorithm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MyStruct ListOfPlayers[20];
ifstream fin("Players.txt");

// read in the data
for (int i = 0; !fin.eof() ; ++i)
{
  fin >> ListOfPlayers[i].player;
  fin >> ListOfPlayers[i].score;
}

// sort the data
std::sort(ListOfPlayers, ListOfPlayers+20, ListOfPlayers[0]);

// store 
//... 
how do i do sorting in data file handling ?

I am making project (game) in which score is stored. I want to write in a file in such way that it writes only scores of 5 persons. i.e

if score is higher then all other then it should be written at top and if lowest then end of file.

or

if score is between 1-5 then it should resemble lower score and delete the other which is lower then above.

for example, score stored in file is like this :
1. 5000
2. 4500
3. 4000
4. 3500
5. 3000

after playing game it should write like this if score is = 3800
1. 5000
2. 4500
3. 4000
4. 3800
5. 3500

thank you.
This is what i did i dont know where do i do changes for
I am making project (game) in which score is stored. I want to write in a file in such way that it writes only scores of 5 persons. i.e

if score is higher then all other then it should be written at top and if lowest then end of file.

or

if score is between 1-5 then it should resemble lower score and delete the other which is lower then above.

for example, score stored in file is like this :
1. 5000
2. 4500
3. 4000
4. 3500
5. 3000

after playing game it should write like this if score is = 3800
1. 5000
2. 4500
3. 4000
4. 3800
5. 3500

thank you.

the code is
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
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class score
{ private:
   char name[20];
   char place[20];
   int score;
   public: 
    void getdata()
   {	cout<<"name \n";
      	gets(name);
	cout<<"place \n";
	gets(place);
	cout<<"score \n";
	cin>>score;
    }

  void display()
  {	cout<<name;
	cout<<endl;
	cout<<place;
	cout<<endl;
	cout<<score;
} 

int getscore()
{ return score; }

}s,c;

void main()
{clrscr();
ifstream ifs("score.txt",ios::in);
ofstream ofs("temp.txt",ios::out);
char last='y';
while(!ifs.eof())
{ s.getdata();
ifs.read((char*) &c,sizeof(c));
	if(s.getscore()<=c.getscore())
	{ofs.write((char*) &s,sizeof(s));
		last = 'n';
		break;
	}
else
ofs.write((char*) &c,sizeof(c));
}

if(last=='y')
ofs.write((char*)&s,sizeof(s));
else
if(!ifs.eof())
{	 while(!ifs.eof())
	{ifs.read((char*) &c,sizeof(c));
	 ofs.write((char*) &c,sizeof(c));
	}
}
ifs.close();
ofs.close();
remove("score.txt");
rename("temp.txt" , "score.txt");
ifs.open("score.txt");
cout<<"now file has \n";
while(!ifs.eof())
{ ifs.read((char*) &c,sizeof(c));
	if(ifs.eof())
	break;
	c.display();
}
ifs.close();
}


it creates record for 1 person i want to make it for 5 persons
Topic archived. No new replies allowed.