Author |
Message |
Dark_Nexus Newbie

Age:39 Gender: Joined: Jan 02 2004 Posts: 23 Offline
|
Posted: 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...
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  _________________ "In fact, about the only reasonable holiday is 4th of July (US). We celebrate something that actually happened by blowing shit up."
-Mr Ekted |
|
Back to top |
|
 |
Doggeti Server Help Squatter

Age:40 Gender: Joined: Jan 12 2003 Posts: 297 Location: Germany Offline
|
Posted: 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. _________________ Expect the worst but hope for the best. |
|
Back to top |
|
 |
Dark_Nexus Newbie

Age:39 Gender: Joined: Jan 02 2004 Posts: 23 Offline
|
Posted: Sun Feb 29, 2004 1:21 am Post subject: |
 |
|
|
|
GetPrivateProfileString is a standard library function. |
|
Back to top |
|
 |
Guest
Offline
|
|
Back to top |
|
 |
NightHawk Newbie
Joined: Dec 09 2003 Posts: 20 Location: Sabotage'd Offline
|
Posted: 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 |
|
Back to top |
|
 |
Dark_Nexus Newbie

Age:39 Gender: Joined: Jan 02 2004 Posts: 23 Offline
|
Posted: 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
okies here is my modified function, but i have a question of efficency now,
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?
Last edited by Dark_Nexus on Sun Feb 29, 2004 1:54 am, edited 1 time in total |
|
Back to top |
|
 |
Guest
Offline
|
Posted: Sun Feb 29, 2004 1:54 am Post subject: |
 |
|
|
|
"" and strlen method works, where as the 0 method doesn't. i just tried it. |
|
Back to top |
|
 |
Dark_Nexus Newbie

Age:39 Gender: Joined: Jan 02 2004 Posts: 23 Offline
|
Posted: Sun Feb 29, 2004 2:00 am Post subject: |
 |
|
|
|
hmmm for somereason sizeof is not working there anyways still a question of efficency, would if (!strcmp(name,"")) or if (STRLEN(name)) be faster?
Last edited by Dark_Nexus on Sun Feb 29, 2004 2:02 am, edited 1 time in total |
|
Back to top |
|
 |
Guest
Offline
|
Posted: 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 |
|
Back to top |
|
 |
Guest
Offline
|
Posted: 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 |
|
Back to top |
|
 |
Dark_Nexus Newbie

Age:39 Gender: Joined: Jan 02 2004 Posts: 23 Offline
|
Posted: 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 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  |
|
Back to top |
|
 |
Guest
Offline
|
Posted: 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. |
|
Back to top |
|
 |
Dustpuppy Server Help Squatter

Age:40 Gender: Joined: Jan 23 2003 Posts: 215 Location: England Offline
|
Posted: Mon Mar 01, 2004 8:46 pm Post subject: |
 |
|
|
|
Any of
if (*name)
if (name[0])
if (strlen(name))
should work. _________________
 |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Mar 01, 2004 10:59 pm Post subject: |
 |
|
|
|
OMG! If you people are going to use C, understand the language first. Jesus Christ! _________________ 4,691 irradiated haggis! |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: 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. _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: 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? |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: 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. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: 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. |
|
Back to top |
|
 |
|