Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

HowcanIsortbinarydatainfileinC

I was working on a game for which I have to create a leaderboard. When I tried sorting the file (written in Binary Mode) my laptop freezed and my Data.txt became a 4gb text file. This is my first project in C++; And the Players will be sorted on the basis of time they have taken less to more.

Here are the methods which I created.

Get Total Records :-

int TotalRecords()
{
   fstream Fobj;
   Fobj.open("E://Mygame//Player Data//Data.txt");
   int FileSize,Rec=0;
   size_t len;
   Fobj.seekg(0,ios::end);
   FileSize=Fobj.tellg();
   Fobj.seekg(0,ios::beg);
   while(Fobj.tellg()!=FileSize)
   {
       Fobj.read((char *)&len,sizeof(size_t));
       Fobj.seekg(len+(2*sizeof(int)),ios::cur);
       Rec++;
   }
   Fobj.close();
   return Rec;
}

Get Input:-

void Input(Player& N)
{
    fstream Fobj;
    size_t len=N.Name.size();
    Fobj.open("E://Mygame//Player Data//Data.txt",ios::out|ios::binary|ios::app);
    Fobj.seekp(0,ios::end);
    Fobj.write((char *)&len,sizeof(size_t));
    Fobj.write(&N.Name[0],len);
    Fobj.write((char *)&N.min,sizeof(int));
    Fobj.write((char *)&N.second,sizeof(int));
    Fobj.close();
}

Sort the file:

void Sort()
{
    fstream Fobj;
    Fobj.open("E://Mygame//Player Data//Data.txt",ios::in|ios::binary|ios::out);
    string tempName,temp1Name;
    int tempMin,tempSecond,tempTime;size_t tempLen;
    int temp1Min,temp1Second,temp1Time;size_t temp1Len;
    int FileSize,T=TotalRecords();
    Fobj.seekp(0,ios::end);
    FileSize=Fobj.tellp();
    Fobj.seekg(0,ios::beg);
    int Count=0;
    for(int i=0;i<T-1;i++)
    {
        for(int j=0;j<T-i-1;j++)
        {
            Fobj.read((char *)&tempLen,sizeof(size_t));
            Fobj.read(&tempName[0],tempLen);
            Fobj.read((char *)&tempMin,sizeof(int));
            Fobj.read((char *)&tempSecond,sizeof(int));
            tempTime=tempMin*60 + tempSecond;

            Fobj.read((char *)&temp1Len,sizeof(size_t));
            Fobj.read(&temp1Name[0],temp1Len);
            Fobj.read((char *)&temp1Min,sizeof(int));
            Fobj.read((char *)&temp1Second,sizeof(int));
            temp1Time=temp1Min*60 + temp1Second;


            if(tempTime>temp1Time)
            {
                Fobj.seekg(-1*(tempLen+temp1Len+(4*sizeof(int))+(2*sizeof(size_t))),ios::cur);

                Fobj.write((char *)&temp1Len,sizeof(size_t));
                Fobj.write(&temp1Name[0],temp1Len);
                Fobj.write((char *)&temp1Min,sizeof(int));
                Fobj.write((char *)&temp1Second,sizeof(int));

                Fobj.write((char *)&tempLen,sizeof(size_t));
                Fobj.write(&tempName[0],tempLen);
                Fobj.write((char *)&tempMin,sizeof(int));
                Fobj.write((char *)&tempSecond,sizeof(int));

                Fobj.seekg(-1*(tempLen+(2*sizeof(int))+sizeof(size_t)),ios::cur);
            }
         }
    }
    Fobj.close();
}

Comments