Server Help

Bot Questions - Reading .ini files

Dark_Nexus - Sun Feb 29, 2004 12:37 am
Post subject: Reading .ini files
okies, here is the format of my .ini

[Player Name]
CurrentQuestion=1
Status=N/A

Example...

[Dark_Nexus]
CurrentQuestion=1
Status=fired.

here is my code...

Code: Show/Hide
bool botInfo::check_name(char* name0)
{
   GetPrivateProfileString(name0,"CurrentQuestion",0,name,32,path);
   if (name) return true;
   else return false;   
}


path = the path to the .ini file (C:\My Documents....ect)
name = a 32 character buffer (char name[32])
name0 = the name that that the player typed in !read <player name>

I call it in command.cpp when a player types !read <palyer name>
the function gets called like check_name(c->final);

The function simply reads throught the .ini file and checks for that players name. If it can't find it, it should put nothing into the buffer, and then return false, if it does find something, it should put the number following "CurrentQuestion" into the buffer and then return true. But the function always return true, even if i type !read bleh, and bleh does not exist in the .ini file. i really need help icon_sad.gif
Doggeti - Sun Feb 29, 2004 1:16 am
Post subject:
I don't understand why you need two functions. I would put all the code from GetPrivateProfileString into check_name and then delete GetPrivate...

However I think you should also post GetPrivateProfileString.
Dark_Nexus - Sun Feb 29, 2004 1:21 am
Post subject:
GetPrivateProfileString is a standard library function.
Anonymous - Sun Feb 29, 2004 1:42 am
Post subject:
Use
Code: Show/Hide
GetPrivateProfileString(name0, "CurrentQuestion", "", name, 32, path);

and then check if the strlen of name is zero or not.

Also, name should be of size 33 if you're going to possibly copy up to 32 chars, because you need to leave room for the terminating null character. You should also check that name0 isn't null if you don't already check it before you call the function (in case the user just types !read)
NightHawk - Sun Feb 29, 2004 1:44 am
Post subject:
name can actually be 32, beacuse that parameter is meant to be the size of the buffer
Dark_Nexus - Sun Feb 29, 2004 1:50 am
Post subject:
actually from what i read on msdn, "" will not work because the function insters a null character after the string to eliminate blanks like that, so i think i just have to put 0.

and in command.cpp i already check to see if c->checkParam("") to see if they entered a name icon_smile.gif

okies here is my modified function, but i have a question of efficency now,

Code: Show/Hide
bool botInfo::check_name(char* name0)
{
   GetPrivateProfileString(name0,"CurrentQuestion",0,name,32,path);
   if (sizeof(name)>0) return true;
   else return false;   
}


would if (!strcmp(name,"")) return false, be quicker?
Anonymous - Sun Feb 29, 2004 1:54 am
Post subject:
"" and strlen method works, where as the 0 method doesn't. i just tried it.
Dark_Nexus - Sun Feb 29, 2004 2:00 am
Post subject:
hmmm for somereason sizeof is not working there new_let_it_all_out.gif anyways still a question of efficency, would if (!strcmp(name,"")) or if (STRLEN(name)) be faster?
Anonymous - Sun Feb 29, 2004 2:00 am
Post subject:
oh you've edited your post and are going to use strlen with 0 - ok that works as well.

you can just use

if (!strlen(name)) return false;
else return true;

or:
return (bool) strlen(name);

but the second one isn't super good style imo
Anonymous - Sun Feb 29, 2004 2:05 am
Post subject:
if you wanted to get super efficient, if (name[0]) would be the most efficient of all. but in these days of Pentium 4s, it doesnt really matter
Dark_Nexus - Sun Feb 29, 2004 2:07 am
Post subject:
bleh i think i was doing something similar to that, but i think if i do
Code: Show/Hide
if (name) return true;
was checking the entire array, instead of just the first character, anways, thanks a lot for your help, i really don't understand how to use GetPrivateProfileString and the like that well, and you guys really helped me out icon_biggrin.gif
Anonymous - Sun Feb 29, 2004 2:17 am
Post subject:
since you said name was a char name[32], using if (name) wouldn't work. 'name' is the address of the start of the string, and since name isn't dynamically allocated (ie it's memory address will never change), if (name) is always going to be true.
Dustpuppy - Mon Mar 01, 2004 8:46 pm
Post subject:
Any of

if (*name)
if (name[0])
if (strlen(name))

should work.
Mr Ekted - Mon Mar 01, 2004 10:59 pm
Post subject:
OMG! If you people are going to use C, understand the language first. Jesus Christ!
Dr Brain - Mon Mar 01, 2004 11:21 pm
Post subject:
Mr Ekted wrote:
OMG! If you people are going to use C, understand the language first. Jesus Christ!


I don't think that what you said is exactly true. It should be "Don't Post C until you understand the language first.
Mr Ekted - Tue Mar 02, 2004 12:08 am
Post subject:
No. I meant don't use it. Every time there's a thread about something to do with code, half the people are just taking guess about how the language works, and some of them are actually trying to make stuff. How about read a book first?
50% Packetloss - Tue Mar 02, 2004 2:33 am
Post subject:
I didnt read much, just pointing out a small thing that bugs me.

Do NOT do if(blah){return true;} else {return false;} . This is ghey and you need to just make a variable. bool Bob=false; and if the function turns out to be true, make bob=true; and then return bob; at the end of the function. Placing returns in the middle of functions is confusing. A bool is 1 byte large, it will be a lot less hassle to just declare the variable and then return it at the end of the function.
Mr Ekted - Tue Mar 02, 2004 3:05 pm
Post subject:
Agreed. Never return in the middle of a function. It is wrong, and can lead to nasty bugs like resource/memory leaks, unclosed files, etc.

Also, if you want to be picky, int is better than bool. It is much faster under Intel (Windows, Linux) to read/write a full 32-bit value than an 8-bit value. Using bool saves data space if there are thousands of them as public/static data, but on the stack they pretty much have no benefit.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group