|
Server Help Community forums for Subgame, ASSS, and bots
|
Author |
Message |
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Mon Apr 07, 2008 7:08 pm Post subject: make c++ read from ini files |
|
|
|
|
any one know any good tutorials on how to make c++ read from ini files for begginers? and doesnt even have to be ini files anything where a variable can be read from file like
x=5
name=john
game= continuum
so that c++ would read from file and input variables put.. |
|
Back to top |
|
|
CypherJF I gargle nitroglycerin
Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Mon Apr 07, 2008 8:41 pm Post subject: |
|
|
|
|
Does this help? Not sure if it's correct, or where I got it from.
//
String getIni(String section, String key, String fname);
void writeIni(String section, String key, String value, String fname);
//
|
#include "spawn.h"
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
const int BUFFER_LEN = 256;
static char buffer[BUFFER_LEN];
static char path[BUFFER_LEN];
String botInfo::getIni(String section, String key, String fname)
{
GetCurrentDirectory(BUFFER_LEN - 64, path);
strcat(path, fname);
GetPrivateProfileString(section, key, '\0', buffer, BUFFER_LEN, path);
return (String)buffer;
}
void botInfo::writeIni(String section, String key, String value, String fname)
{
GetCurrentDirectory(BUFFER_LEN - 64, path);
strcat(path, fname);
WritePrivateProfileString(section, key, value, path);
}
|
_________________ Performance is often the art of cheating carefully. - James Gosling |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Mon Apr 07, 2008 9:04 pm Post subject: |
|
|
|
|
hm is there a simpler explanation? and it doesnt have to deal with mervbots, like for a random program ? |
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Tue Apr 08, 2008 4:18 pm Post subject: |
|
|
|
|
I think that tutorial for ini files is only with MervBots since when i tried it, it didnt compile and compiler said that CMPSTART' was undeclared
here is source:
#include "stdlib.h" // for atoi()
#include <windows.h> // for GetPrivateProfileString()
#include <iostream>
#include <fstream>
using namespace std;
void p()
{
system("pause");
}
int main()
{
ifstream conf;
conf.open("conf.txt");
if (!conf)
{
cout<<"\n\n\n\t\t\tFile conf.txt doesn't exist!\n\n\t\t\t";
p();
return 0;
}
char line[256];
while (conf.getline(line, 256))
{
if (CMPSTART("player=", line))
{
int player_age = atoi(&(line[7]));
}
}
}
|
here are errors
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Owner\Desktop\Programing\c++\ini file reader\inifilereader.cpp" -o "C:\Documents and Settings\Owner\Desktop\Programing\c++\ini file reader\inifilereader.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Documents and Settings\Owner\Desktop\Programing\c++\ini file reader\inifilereader.cpp: In function `int main()':
C:\Documents and Settings\Owner\Desktop\Programing\c++\ini file reader\inifilereader.cpp:27: error: `CMPSTART' undeclared (first use this function)
C:\Documents and Settings\Owner\Desktop\Programing\c++\ini file reader\inifilereader.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
Execution terminated
|
|
|
Back to top |
|
|
k0zy Server Help Squatter
Gender: Joined: Jan 11 2003 Posts: 571 Location: Germany Offline
|
|
Back to top |
|
|
Doc Flabby Server Help Squatter
Joined: Feb 26 2006 Posts: 636 Offline
|
|
Back to top |
|
|
Samapico No, these DO NOT look like penises, ok?
Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Tue Apr 08, 2008 5:04 pm Post subject: |
|
|
|
|
I made myself some functions using windows APIs, from other plugins:
String readINIkeyString(char *section, char *key, char *_default, char *path);
String readINIkeyLongString(char *section, char *key, char *_default, char *path);
int readINIkeyInt(char *section, char *key, int _default, char *path);
String readINIkeyString(char *section, char *key, char *_default, char *path)
{
char resultbuffer[64];
GetPrivateProfileStringA(section, key, _default, resultbuffer, 64, path);
resultbuffer[63] = '\0';
return (String) resultbuffer;
}
String readINIkeyLongString(char *section, char *key, char *_default, char *path)
{
char resultbuffer[256];
GetPrivateProfileStringA(section, key, _default, resultbuffer, 256, path);
resultbuffer[255] = '\0';
return (String) resultbuffer;
}
int readINIkeyInt(char *section, char *key, int _default, char *path)
{
return (int)GetPrivateProfileIntA(section, key, _default, path);
} |
You need <winbase.h> included, but if you put these things in spawn.h and spawn.cpp, it should already be included _________________ (Insert a bunch of dead links here) |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Tue Apr 08, 2008 7:26 pm Post subject: |
|
|
|
|
ok i tried this and forsome reason all values return with the default not whats in the file
file
[Names]
player=hellzlaker
zone=Counter Strike E
[Ages]
player=15
zone=4 |
source
#include <stdlib.h> // for atoi()
#include <windows.h> // for GetPrivateProfileString()
#include <iostream>
#include <fstream>
#include <winbase.h>
#include <string>
using namespace std;
void p()
{
system("pause");
}
string readINIkeyString(char *section, char *key, char *_default, char *path)
{
char resultbuffer[64];
GetPrivateProfileStringA(section, key, _default, resultbuffer, 64, path);
resultbuffer[63] = '\0';
return (string) resultbuffer;
}
string readINIkeyLongString(char *section, char *key, char *_default, char *path)
{
char resultbuffer[256];
GetPrivateProfileStringA(section, key, _default, resultbuffer, 256, path);
resultbuffer[255] = '\0';
return (string) resultbuffer;
}
int readINIkeyInt(char *section, char *key, int _default, char *path)
{
return (int)GetPrivateProfileIntA(section, key, _default, path);
}
int main()
{
string playername=readINIkeyString("Names","player","error","conf.txt");
string zonename=readINIkeyString("Names","zone","error","conf.txt");
int playerage=readINIkeyInt("Ages","player",0,"conf.txt");
int zoneage=readINIkeyInt("Ages","zone",0,"conf.txt");
cout<<"\n\tPlayer name : "<<playername;
cout<<"\n\tPlayer age : "<<playerage;
cout<<"\n\tZone name : "<<zonename;
cout<<"\n\tZone age : "<<zoneage<<"\n\n\t\t";
p();
return 0;
}
|
|
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Tue Apr 08, 2008 9:18 pm Post subject: |
|
|
|
|
replace
if (CMPSTART("player=", line)) |
with
if (strstr(line,"player=") == line) |
and #include "string.h" |
|
Back to top |
|
|
Samapico No, these DO NOT look like penises, ok?
Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Tue Apr 08, 2008 11:32 pm Post subject: |
|
|
|
|
string playername=readINIkeyString("Names","player","error","conf.txt");
hmmm... yeah for some reason, I remember I had to give it absolute paths... Like "C:\blahblah\conf.txt" instead of just "conf.txt". For this I had a function that returns the current directory of the exe or dll.
String CurrentDirectory()
{
char path[520];
GetCurrentDirectoryA(520, path);
strcat(path, "\\");
return (String) path;
} |
string path = CurrentDirectory() + "conf.ini";
string playername=readINIkeyString("Names","player","error",path); |
should work... All my code was using 'String', not 'string' though, so I don't know if there are any typecasting differences between them... |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Wed Apr 09, 2008 5:48 pm Post subject: |
|
|
|
|
thanks it worked, and I used (string) instead of (String) since Dev c++ told me (String) is undeclared... |
|
Back to top |
|
|
Samapico No, these DO NOT look like penises, ok?
Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Thu Apr 10, 2008 11:54 am Post subject: |
|
|
|
|
well, String is declared in the mervbot core somewhere. If you use it in spawn.cpp, spawn.h, or command.cpp, it will be declared |
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Apr 10, 2008 8:51 pm Post subject: |
|
|
|
|
it's usually frowned upon to return structures or classes (like string or String) or pass them into functions as parameters (because it copies them implicitly). |
|
Back to top |
|
|
Samapico No, these DO NOT look like penises, ok?
Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Fri Apr 11, 2008 1:52 am Post subject: |
|
|
|
|
hmm, good point I guess... I could improve that |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Wed Apr 16, 2008 8:43 pm Post subject: |
|
|
|
|
samapico your functions worked in my stand alones exes but when i tried to use then in merv i had a bit confusion, like where to declare variables for an example i tried this
String PointsName1 = readINIkeyString("conf","PointsName1","points",CurrentDirectory()+"DCmissions.ini");
else if (c->check(PointsName1) )
{
int money = p->score.killPoints+p->score.flagPoints;
sendPrivate(p,(String)p->name+", you have "+(String)money ); |
the compile errors were 1>------ Build started: Project: default, Configuration: Debug Win32 ------
1>Compiling...
1>command.cpp
1>c:\documents and settings\owner\desktop\programing\c++\dc missons\dcmissions\command.cpp(203) : error C2181: illegal else without matching if
1>Build log was saved at "file://c:\Documents and Settings\Owner\Desktop\Programing\c++\DC missons\DCmissions\Debug\BuildLog.htm"
1>default - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== |
i think compiler messed up since when i remove the String PlayerName = etc.. if compiles fine, i tried moving the String PlayerName = .. before the if, but some other wierd error came up
and i have declared the function in spawn.h and spawn.cpp |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Mon Jul 21, 2008 11:50 am Post subject: |
|
|
|
|
sorry old topic but i reformatted computer and lost all the info that i had and i got this to work last time but now i get this error for some reason
C:\Documents and Settings\Owner\Desktop\programing\tests\Untitled1.cpp:61: error: cannot convert `std::string' to `char*' for argument `4' to `std::string readINIkeyString(char*, char*, char*, char*)'
C:\Documents and Settings\Owner\Desktop\programing\tests\Untitled1.cpp:62: error: cannot convert `std::string' to `char*' for argument `4' to `int readINIkeyInt(char*, char*, int, char*)' |
This is the code #include <windows.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
void visual()
{
SetConsoleTitle("TEST");
system("color f0");
system("cls");
}
void c()
{
system("cls");
}
string CurrentDirectory()
{
char path[520];
GetCurrentDirectoryA(520, path);
strcat(path, "\\");
return (string)path;
}
string readINIkeyString(char *section, char *key, char *_default, char *path)
{
char resultbuffer[64];
GetPrivateProfileStringA(section, key, _default, resultbuffer, 64, path);
resultbuffer[63] = '\0';
return (string) resultbuffer;
}
string readINIkeyLongString(char *section, char *key, char *_default, char *path)
{
char resultbuffer[256];
GetPrivateProfileStringA(section, key, _default, resultbuffer, 256, path);
resultbuffer[255] = '\0';
return (string) resultbuffer;
}
int readINIkeyInt(char *section, char *key, int _default, char *path)
{
return (int)GetPrivateProfileIntA(section, key, _default, path);
}
int main()
{
visual();
system("pause");
c();
string path = CurrentDirectory()+"test.ini";
string port = readINIkeyString("main","port","ERROR CAN'T READ!",path);
int intport = readINIkeyInt("main","port",-99999,path);
cout<<"\n\n\tString Port\t"<<port<<"\n\tInt Port\t"<<intport<<"\n\n\n\n\n\t\t";
system("pause");
return 0;
} |
I tried converting string to char but still didn't work... |
|
Back to top |
|
|
Cheese Wow Cheese is so helpful!
Joined: Mar 18 2007 Posts: 1017 Offline
|
Posted: Mon Jul 21, 2008 1:42 pm Post subject: |
|
|
|
|
how do u put a [directory] ip= type thing in there?
where it reads a string with an unknown # of name,name2,etc? _________________ SSC Distension Owner
SSCU Trench Wars Developer |
|
Back to top |
|
|
Samapico No, these DO NOT look like penises, ok?
Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Mon Jul 21, 2008 10:55 pm Post subject: |
|
|
|
|
2 ways:
[section]
Something1=stuff
Something2=blah
Something3=oink |
You keep reading from the ini by incrementing a variable until it returns you "" (or whatever the default value is)
Or:
[section]
Something=stuff;blah;oink |
Use whatever separator you want, and use the split function of the string. (Mervbot's String class has it; there must be something similar with the standard string class, or you can go in merv's code and take the split function to make your own) Basically it will take a substring up to a specified character and return you the left part, while removing that part from the string itself. So you keep doing this until your string is empty.
Example:
String somestring="stuff;blah;oink";
do
{
String somepart = somestring.split(';');
//somepart would be "stuff" the first time, then "blah", then "oink", then ""
} while (!somestring.isEmpty()); |
|
|
Back to top |
|
|
Purge Episode I > Eposide III Jar-Jar is kool
Age:34 Gender: Joined: Sep 08 2004 Posts: 2018 Offline
|
Posted: Mon Jul 21, 2008 11:03 pm Post subject: |
|
|
|
|
Using WinAPI would be much easier in your case. Check MSDN for the GetPrivateProfileString() function. |
|
Back to top |
|
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Software by php BB © php BB Group Server Load: 1202 page(s) served in previous 5 minutes.
|