I´m working on a hiscorelist for my DirectX-game. I don''t have much experience and this is my first game (a Tetris clone). I want to have a Hiscorelist in my game and I have figured most of it out but there still are some problems...
Now I´m working on a DOS-project (easier to learn that way) and I am going to rewrite it to work in my Windows Game in a while. I just want to get it to work here before I move it into my game. OK This is what I know:
1. I can compare my current score with the entries in the hiscorelist (if i write the list into my program, not from file)
2. I can save my hiscore to a file.
3. I can show it on screen.
4. I can load the names of the players from the file and store them in variables.
What I don''t know is this:
1. How to load the score from the file and store it in a variable as integers (I need to be able to compare the scores later when to decide if there''s a new hiscore or not...)
The problem is that my books don''t tell me anything about these things, and I have browsed through the MSDN to find the answer, but I''m feeling lost here...
Perhaps if I''ll show you a sample of my LoadHiscore()-function, someone can give me some advice?
bool LoadHiscore()
{
FILE *fp;
char tempchar[4];
int tempint[6];
// Open file for reading
fp = fopen("hiscore.dat", "r");
if (!fp) return false;
// Process file and read what''s in it
for (ctr = 0; ctr < MAX_POSITIONS; ctr++)
{
for (int ch = 0; ch < 4; ch++)
{
char temp = fgetc(fp);
tempchar[ch] = temp;
}
tempchar[3] = ''\0'';
strcpy(PlayerData[ctr].name, tempchar);
}
I don''t know if this is considered "ugly" or not, but it works, so I''m happy :-)
Well, it works on the Players names, but how can I make it work for the scores? I want the scores stored in PlayerData[ctr].score (It''s a array of type HISCORE (a struct, where score is like this: int score))
As you can see: I read the file char by char, and I just don''t know how to do this when it comes to integers! I can store the score in the array, but then it''s not an integer but a string!!!
If I cast them, the output isn''t what I expect...
Well, hope you guys can help me a bit with this...
Thanks
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn''t become what you should..."
Loading an integer from file in C++...
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn't become what you should..."
"You have to be who you are, when you didn't become what you should..."
December 22, 2001 07:22 PM
int atoi(const char *nptr);
so if your score is in ''tempint'' (I guess it is), you''d just have this :
// read the score in a C string (char array[])
// assume it''s call ''foo''
PlayerData[ctr].score = atoi(foo);
and that''s it. Of course it raises its own share of problems. You must be sure than ''foo'' actually contains a number (and all the other problems related to C string: overflows, etc. Remember to do bound checking).
so if your score is in ''tempint'' (I guess it is), you''d just have this :
// read the score in a C string (char array[])
// assume it''s call ''foo''
PlayerData[ctr].score = atoi(foo);
and that''s it. Of course it raises its own share of problems. You must be sure than ''foo'' actually contains a number (and all the other problems related to C string: overflows, etc. Remember to do bound checking).
December 22, 2001 07:24 PM
quote: Original post by Anonymous Poster
int atoi(const char *nptr);
so if your score is in ''tempint'' (I guess it is), you''d just have this :
// read the score in a C string (char array[])
// assume it''s call ''foo''
PlayerData[ctr].score = atoi(foo);
and that''s it. Of course it raises its own share of problems. You must be sure than ''foo'' actually contains a number (and all the other problems related to C string: overflows, etc. Remember to do bound checking).
Ugh I must be tired.
// read the score in tempint
// and convert to int
PlayerData[ctr].score = atoi(tempint);
Hope this helps... And than it''s correct this time...
i think chars are the same as unsigned ints so read the file like it was chars but then just copy the char values into unsigned int values. Then test if any of them arn''t in the range 0-9. If they are, there is an error. Here''s how to copy the char to unsigned int
unsigned int score[6]= fgetc(fp);
int realscore=score[0]+(score[1]*10)+(score[2]*100)+(score[3]*1000 and so on until the score is a real integer and not individual values.
unsigned int score[6]= fgetc(fp);
int realscore=score[0]+(score[1]*10)+(score[2]*100)+(score[3]*1000 and so on until the score is a real integer and not individual values.
quote: Original post by GoomfGoomfDweeda
I1. How to load the score from the file and store it in a variable as integers (I need to be able to compare the scores later when to decide if there''s a new hiscore or not...)
You say C++, so actually use C++:
#include <fstream>#include <iostream>#include <string>using std::cin;using std::cout;using std::endl;using std::ifstream;using std::string;int main(int argc, char *argv[]){ string filename; cout << "Please enter the name of the file with the integer data: "; cin >> filename; // // open the file for input ifstream ifile; ifile.open(filename.c_str(), ios::in); if(ifile.fail()) { return -1; } int i;//***// This is all it takes to read an integer from a file: ifile >> i;//*** cout << "The integer read from the file was: " << i << endl; return 0;}
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
December 23, 2001 01:51 AM
The first reply was correct, just be sure to #include <cstdlib> or <stdlib.h> depending on your compiler. Also consider the character handling library, <cctype> or <ctype.h> to make sure the atoi() is passed a valid argument
int isdigit (int c) //returns TRUE if ''c'' an integer 0-9, else FALSE
int isdigit (int c) //returns TRUE if ''c'' an integer 0-9, else FALSE
Hey thanks!
I''ll try this. Just wanted you to know that I''m appreciating all your help! Perhaps this will work. We''ll see what happens...
Sure I will post if I get stuck again, but now I think that I can make it work!
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn''t become what you should..."
I''ll try this. Just wanted you to know that I''m appreciating all your help! Perhaps this will work. We''ll see what happens...
Sure I will post if I get stuck again, but now I think that I can make it work!
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn''t become what you should..."
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn't become what you should..."
"You have to be who you are, when you didn't become what you should..."
You could use the fread and fwrite functions. They are so much better than fputs and fgetc. Go here, i have a tut on file io but there''s a part on fread and fwrite at the bottom.
kmsixpence!
Thanks for the tutorial! I´ll try that right away!
And to all the others: thank you too! I am working on your suggestions right now. This starts to make sense now!
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn''t become what you should..."
Thanks for the tutorial! I´ll try that right away!
And to all the others: thank you too! I am working on your suggestions right now. This starts to make sense now!
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn''t become what you should..."
GoomfGoomfDweeda (Well, what can I say ? All other cool names are busy!)
"You have to be who you are, when you didn't become what you should..."
"You have to be who you are, when you didn't become what you should..."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement