Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
make c++ read from ini files

 
Post new topic   Reply to topic Printable version
 View previous topic  Throw your windows around Post :: Post something wierd in c++  View next topic  
Author Message
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Mon Apr 07, 2008 7:08 pm    Post subject: make c++ read from ini files Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
CypherJF
I gargle nitroglycerin


Gender:Gender:Male
Joined: Aug 14 2003
Posts: 2579
Location: USA
Offline

PostPosted: Mon Apr 07, 2008 8:41 pm    Post subject: Reply to topic Reply with quote

Does this help? Not sure if it's correct, or where I got it from.

Code: Show/Hide

   //
   String getIni(String section, String key, String fname);
   void writeIni(String section, String key, String value, String fname);
   //


Code: Show/Hide

#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
View users profile Send private message Add User to Ignore List
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Mon Apr 07, 2008 9:04 pm    Post subject: Reply to topic Reply with quote

hm is there a simpler explanation? and it doesnt have to deal with mervbots, like for a random program ?
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Mon Apr 07, 2008 9:06 pm    Post subject: Reply to topic Reply with quote

http://wiki.minegoboom.com/index.php/MERVBot_Tutorial#Input.2FOutput_to_files
_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Tue Apr 08, 2008 4:18 pm    Post subject: Reply to topic Reply with quote

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:
Code: Show/Hide

#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
Code: Show/Hide
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
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
k0zy
Server Help Squatter


Gender:Gender:Male
Joined: Jan 11 2003
Posts: 571
Location: Germany
Offline

PostPosted: Tue Apr 08, 2008 4:45 pm    Post subject: Reply to topic Reply with quote

I guess it doesn't work because CMPSTART is some mervbot macro.

Try strncmp instead.
http://www.cplusplus.com/reference/clibrary/cstring/strncmp.html
_________________
It's a shark! Oh my god! Unbelievable!
Back to top
View users profile Send private message Add User to Ignore List
Doc Flabby
Server Help Squatter


Joined: Feb 26 2006
Posts: 636
Offline

PostPosted: Tue Apr 08, 2008 4:48 pm    Post subject: Reply to topic Reply with quote

http://code.jellycan.com/simpleini/

or read up in the windows api Got a complete example in this link
http://msdn2.microsoft.com/en-us/library/ms725501(VS.85).aspx
_________________
Rediscover online gaming. Get Subspace | STF The future...prehaps
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Tue Apr 08, 2008 5:04 pm    Post subject: Reply to topic Reply with quote

I made myself some functions using windows APIs, from other plugins:

Code: Show/Hide

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
_________________
DCME co-developer
17th Parallel Head Sysop
Subspace: The Future
Back to top
View users profile Send private message Add User to Ignore List
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Tue Apr 08, 2008 7:26 pm    Post subject: Reply to topic Reply with quote

ok i tried this and forsome reason all values return with the default not whats in the file

file
Code: Show/Hide

[Names]
player=hellzlaker
zone=Counter Strike E

[Ages]
player=15
zone=4


source
Code: Show/Hide

#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
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Tue Apr 08, 2008 9:18 pm    Post subject: Reply to topic Reply with quote

replace
Code: Show/Hide
if (CMPSTART("player=", line))

with
Code: Show/Hide
if (strstr(line,"player=") == line)


and #include "string.h"
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Tue Apr 08, 2008 11:32 pm    Post subject: Reply to topic Reply with quote

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.

Code: Show/Hide
String CurrentDirectory()
{
   char path[520];
   GetCurrentDirectoryA(520, path);
   strcat(path, "\\");
   return (String) path;
}


Code: Show/Hide
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
View users profile Send private message Add User to Ignore List
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Wed Apr 09, 2008 5:48 pm    Post subject: Reply to topic Reply with quote

thanks it worked, and I used (string) instead of (String) since Dev c++ told me (String) is undeclared...
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Thu Apr 10, 2008 11:54 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Apr 10, 2008 8:51 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Fri Apr 11, 2008 1:52 am    Post subject: Reply to topic Reply with quote

hmm, good point I guess... I could improve that tongue.gif
Back to top
View users profile Send private message Add User to Ignore List
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Wed Apr 16, 2008 8:43 pm    Post subject: Reply to topic Reply with quote

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
Code: Show/Hide

         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
Code: Show/Hide
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
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Mon Jul 21, 2008 11:50 am    Post subject: Reply to topic Reply with quote

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

Code: Show/Hide
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
Code: Show/Hide
#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
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Cheese
Wow Cheese is so helpful!


Joined: Mar 18 2007
Posts: 1011
Offline

PostPosted: Mon Jul 21, 2008 1:42 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website AIM Address
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Mon Jul 21, 2008 10:55 pm    Post subject: Reply to topic Reply with quote

2 ways:

Code: Show/Hide
[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:
Code: Show/Hide
[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:
Code: Show/Hide
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
View users profile Send private message Add User to Ignore List
Purge
Episode I > Eposide III
Jar-Jar is kool


Age:28
Gender:Gender:Male
Joined: Sep 08 2004
Posts: 2017
Location: 中国
Offline

PostPosted: Mon Jul 21, 2008 11:03 pm    Post subject: Reply to topic Reply with quote

Using WinAPI would be much easier in your case. Check MSDN for the GetPrivateProfileString() function.
Back to top
View users profile Send private message Add User to Ignore List Send email
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 88 page(s) served in previous 5 minutes.

phpBB Created this page in 0.123646 seconds : 45 queries executed (35.7%): GZIP compression disabled