0

I am creating a minesweeper game on turbo c++ and have got that logical part spot on.

Now my aim is to save highscores by time, ascending order(max 10 records). The problem is that I have to use turbo c++ no matter what

To do so, I created a class player which has to members pname, time storing the obvious values. then if file doesn't exist then a new file is created and records are sent into it. No problem here.

The problem is in this part when the file already exists:

player test;
    int flag=-1,count=0; //flag to mark position at which new record will be inserted and count to count total no. of records present
    while(f.read((char*)&test,sizeof(test))&&count<=10) //file is already opened in ifstream mode
    {
        count++;
        if(curr_p.time<=test.time&&flag==-1) //curr_p is the object passed into this fxn and contains the current player's time
        {
            flag=(f.tellg())/sizeof(test)-1;
        }
    }
    f.close();
    f.open("high.dat",ios::binary|ios::in|ios::out);
    if(count<10&&flag==-1)
        flag=count;
    cout<<"\n flag"<<flag<<"\n";
    if(flag!=-1)
    {
        cout<<"\n it's a highscore! now enter your name "<<endl;
        gets(curr_p.pname);
        for(int i=(count-1);i>=flag;i--)
        {
            if(i<10)
            {
                f.seekp((i)*sizeof(test),ios::beg);
                f.read((char*)&test,sizeof(test));
                f.write((char*)&test,sizeof(test));
            }
        }
        f.seekp(flag*sizeof(test),ios::beg);
        f.write((char*)&curr_p,sizeof(test));
    }
    f.close();

The problem here is that all is fine till there are 2 records, for example these are the contents after entering 2 records

   Name                Time                                                     

1. 2nd record          100                                                      

2. 1st record          200  

after entering 3rd record-

   Name                Time                                                     

1. 3rd record          50                                                       

2. 1st record          200                                                      

3. 1st record          200                                                      

4. 2nd record          100  

I can't figure out what's wrong here

EDIT1- Well thanks for the help everyone but I played around with my code a little more and made the number of entries to 11 and with some more changes, somehow, all started working fine. Guess I'm stuck with a 11 player highscore, but well it sounds cooler than a conventional 10 player one. I guess?

  • this comment of mine is a bit off topic but: take a look at mine [Turbo C++ MineSweeper](https://stackoverflow.com/a/45561564/2521214) I coded ages ago for some ideas... It is half automatic (the program solves what it can on its own) it uses mouse as input and IIRC it is in 320x200x256 colors VGA mode. However no file access or score table. For binary file access I always used the `File(Read/Write/Open/Create/Close)` encapsulations of block file operations. I do not remember the correct names of the functions in TCPP anymore. – Spektre Dec 30 '17 at 10:04
  • I think you are perhaps over complicating matters for such a small file. Why not just read the entire file as an array of `player`, update the array, and write the entire thing back to the file? Would probably be faster too since you get linear I/O instead of this jumping around, also much simpler code. – odyss-jii Dec 30 '17 at 10:05
  • IIRC the `fstream` file access had a bug that if any special control code was present in the file the readed/writed data was cut off or corrupted. Direct File access is better for such cases (via DOS interrupt but TCPP had it encapsulated by functions so no need for asm) – Spektre Dec 30 '17 at 10:05

0 Answers0