Hey, thanks for the replies, it turns out I was trying to pass a std::string instead of a c-style string, so it was messing up.
But, I now Have a new problem, This is how my code now looks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
std::string tmpname = "";
std::string tmppass = "";
const char* name = tmpname.c_str();
const char* pass = tmppass.c_str();
int health = -1;
int armour = -1;
int lives = -1;
while( true ) {
Player user;
int vals_read = fscanf(fileHandle,"%s %s %d %d %d",name,pass,health,armour,lives);
if( vals_read == 5 ) {
// process the line
user.setName(name);
user.setPass(pass);
user.setHealth(health);
user.setArmour(armour);
user.setLives(lives);
table.Insert(user.getName(),user);
} else if( feof( fileHandle ) )
break;
}
fclose(fileHandle);
|
As you can see, I read in 2 sets of cstrings and then 3 numbers,all separated by a space. Now, because %s reads until a newline/space (according to what I read on one of the tutorials here), I thought this code should work. But, it doesn't.
My text file now looks like this:
Jack testpass 100 50 4
only 1 line. I found it actually loops twice, the first time it loops, name and pass are correct, but health,armour and lives are still all default -1. The second time it loops, name and pass are equal to 100 and 50 respectively. Thanks for your help :P