Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Little Online Highscore  (Read 4592 times)

0 Members and 1 Guest are viewing this topic.

n0_0ne

  • Newbie
  • *
  • Posts: 5
    • View Profile
Little Online Highscore
« on: April 06, 2009, 10:42:41 am »
Hello,
I'm a totally beginner, when it comes to network and files, so I am having some trouble setting up a online highscore for my game.
I imagined a little protocol with a Int8 at the beginning of each packet, to identify what sort of packet it is (close server, insert new highscore, send highscorelist etc...)... this works somehow...
To test a little bit, I wanted the server to write a new entry into a file, then reading it, creating a std::list with all entries, and then writing all list-entries into std::cout...
Let me first post my code:
Code: [Select]

string sName;
float fScore;
// Read Packet into vars
PacketReceived >> sName >> fScore;

// Open file
fstream ScoreFile("highscore.txt", ios::in | ios::out | ios::binary);
if (ScoreFile.is_open())
cout << "Opened ScoreFile" << endl;

// Create Entry of packet
entry Entry;
Entry.Name = sName;
Entry.Score = fScore;

// Set writepointer to end
ScoreFile.seekp(0, ios::end);

// Write entry into file
ScoreFile << Entry.Name << Entry.Score;
cout << Entry.Name << ": " << Entry.Score << " inserted" << endl;

ScoreFile.seekp(0, ios::beg);
// Create a highscorelist
list<entry> Entries;
while (!ScoreFile.eof())
{
entry Entry;
ScoreFile >> Entry.Name;
ScoreFile >> Entry.Score;
Entries.push_back(Entry);
}

// Write list
cout << endl << "Begin new highscorelist:" << endl;
list<entry>::iterator It = Entries.begin();
while (It != Entries.end())
{
cout << It->Name << ": " << It->Score << endl;
It++;
}

ScoreFile.close();
cout << "Closed ScoreFile" << endl;

///////////////////////////////
// this is the entry struct:
struct entry
{
    string Name;
    float Score;
};

OK, this writes a new entry into the file (at the end), but I am not able to read it in the way I want to...
My std::list will always have only 1 entry, where the entry.Name (which is a string) contains the whole file (xtimes string+float), and a weird entry.Score = -1.9xxxxx. It seems, that the strings aren't zeroterminated, so he will always read the whole file into to entry.Name.

What am I doing wrong?

Edit: I found a way how to do it, just insert a ' ' (space) between string and float... so File >> string will only read until the space and stop!