File Questions
I want to set my game up to load files that keep high scores and preferences and stuff. I know how to load the file but how do I get information from my files? I would like to get it line by line. Say my data file looks like this
GameData.dat
#player score
10054
#bullet holes 1=on 0=off
0
#level
2
is there any magical function that allows you to read the lines of a text file and if the line begins with a # to automatically skip the line?
Visit http://members.xoom.com/ivanickgames
Visit http://members.xoom.com/ivanickgames
As your nick is C++ Freak I assume you're using C++.
Use ifstream::getline() to read a line of text. Then check the first character if, if it is '#' dont parse it.
Edited by - Spellbound on 2/20/00 5:51:13 PM
Use ifstream::getline() to read a line of text. Then check the first character if, if it is '#' dont parse it.
Edited by - Spellbound on 2/20/00 5:51:13 PM
You could always use the Windows API functions for SetPrivateProfileString/GetPrivateProfileString, etc. That''ll handle formatting issues for you. Admittedly it''s deprecated.
Check out fscanf if you''re not C++
FILE *stream;
int variableCount = 0;
char Variabledata[30][200];
char tempstring[200];
// open the game data file
if ((stream = fopen("game.cfg","rt")) == NULL)
{
// MSG can''t find file
}
while ( fscanf(stream,"%s",tempstring) != EOF)
{
if (tempstring[0] != ''#'') //
{
strcpy(Variabledata[variableCount++], tempstring);
}
}
When done, you can atoi() the variables
Cautions
1) I haven''t tested it out and the fscanf() might
chop at every blank-space not every carriage return
so you might have to do ==>
#bullet_holes_1=on_0=off
2) just make sure your variables are in order
Truth is I need to do that myself
ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
http://www.geocities.com/Area51/Atlantis/7739/
FILE *stream;
int variableCount = 0;
char Variabledata[30][200];
char tempstring[200];
// open the game data file
if ((stream = fopen("game.cfg","rt")) == NULL)
{
// MSG can''t find file
}
while ( fscanf(stream,"%s",tempstring) != EOF)
{
if (tempstring[0] != ''#'') //
{
strcpy(Variabledata[variableCount++], tempstring);
}
}
When done, you can atoi() the variables
Cautions
1) I haven''t tested it out and the fscanf() might
chop at every blank-space not every carriage return
so you might have to do ==>
#bullet_holes_1=on_0=off
2) just make sure your variables are in order
Truth is I need to do that myself
ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
http://www.geocities.com/Area51/Atlantis/7739/
fscanf() will probably give you strange hard to understand behavior if you use it like that. You''ll want to use fgets() instead.
Thanks for all the help guys, I just have one more question.
Whats wrong with this function?
int Read_Line(char *string, FILE *fp)
{
while(1)
{
(!fgets(string,80,fp));
return(0);
//error
for(int scan=0;scan {
if(string[scan]!=''#'')
return(1);
else
break;
}
}
return(0);
}
I thought that it would get a line, scheck to see if it began with a # and if it did to get another line until it found a good string. However when I ran my program it used a line that began with a #. Does anybody know how to fix my function?
game.dat
#comment
message
Visit http://members.xoom.com/ivanickgames
Whats wrong with this function?
int Read_Line(char *string, FILE *fp)
{
while(1)
{
(!fgets(string,80,fp));
return(0);
//error
for(int scan=0;scan
if(string[scan]!=''#'')
return(1);
else
break;
}
}
return(0);
}
I thought that it would get a line, scheck to see if it began with a # and if it did to get another line until it found a good string. However when I ran my program it used a line that began with a #. Does anybody know how to fix my function?
game.dat
#comment
message
Visit http://members.xoom.com/ivanickgames
Visit http://members.xoom.com/ivanickgames
Your loop is kind of off. It first checks to see that the first character is not equal to ''#'' and if it isn''t it returns a one. If it is equal to ''#'' it will check the next character, which will be not ''#'' so it will return a 1, saying "use this line". Why not just check the first character? Or are you trying to strip off whitespace?
Okay I changed my function to what I thought would work this time. Here it is.
int Read_Line(char *string, FILE *fp)
{
while(1)
{
(!fgets(string,80,fp));
return(0);
//error
if(string[0]=='!')
{
return(1);
}
}
return(0);
}
I made it check if the first character is a < and if it isn't it goes to the next line. Therefore shouldn't it skip any line that doesn't begin with a ! ? I still get the same results though it prints the comment to the screen. Am I making a stupid mistake again?
New game.dat file
#comment
!message
Visit http://members.xoom.com/ivanickgames
Edited by - C++ Freak on 2/21/00 1:36:44 PM
int Read_Line(char *string, FILE *fp)
{
while(1)
{
(!fgets(string,80,fp));
return(0);
//error
if(string[0]=='!')
{
return(1);
}
}
return(0);
}
I made it check if the first character is a < and if it isn't it goes to the next line. Therefore shouldn't it skip any line that doesn't begin with a ! ? I still get the same results though it prints the comment to the screen. Am I making a stupid mistake again?
New game.dat file
#comment
!message
Visit http://members.xoom.com/ivanickgames
Edited by - C++ Freak on 2/21/00 1:36:44 PM
Visit http://members.xoom.com/ivanickgames
My next thought is then, what''s up with this:
It looks like you''re reading the line and then automatically returning 0. Should there be an if somewhere?
(!fgets(string,80,fp));return(0);
It looks like you''re reading the line and then automatically returning 0. Should there be an if somewhere?
February 21, 2000 01:54 PM
I''m confused.
what does this code do?
(!fgets(string,80,fp));
return(0);
it calls fgets, negates the return value and then it returns 0
shouldn''t this look something like
if (!fgets(string,80,fp)) return(0);
???
what does this code do?
(!fgets(string,80,fp));
return(0);
it calls fgets, negates the return value and then it returns 0
shouldn''t this look something like
if (!fgets(string,80,fp)) return(0);
???
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement