| View previous topic :: View next topic |
| Author |
Message |
slippppppppp Grandmaster Cheater
Reputation: 0
Joined: 08 Aug 2006 Posts: 929
|
Posted: Sun Dec 09, 2007 10:50 pm Post subject: [C++] |
|
|
i need help.
How do i make an ini file,
write and read text to it, ive been googling for 2 hours and it didnt help at all
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
| Back to top |
|
 |
slippppppppp Grandmaster Cheater
Reputation: 0
Joined: 08 Aug 2006 Posts: 929
|
Posted: Sun Dec 09, 2007 10:54 pm Post subject: |
|
|
Well, i already seen that.
and i dont know what the "include" is
to use Tinifile or inifile.
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Dec 10, 2007 1:37 am Post subject: |
|
|
WritePrivateProfileString()
| Code: |
BOOL WINAPI WritePrivateProfileString(
__in LPCTSTR lpAppName,
__in LPCTSTR lpKeyName,
__in LPCTSTR lpString,
__in LPCTSTR lpFileName
);
|
lpAppName = Section Header
lpKeyName = Variable
lpString = Variable Value
lpFileName = location and name of ini
The ini would look something like this
| Code: |
[Section Header]
Variable=Variable Value
|
GetPrivateProfileString()
| Code: |
DWORD WINAPI GetPrivateProfileString(
__in LPCTSTR lpAppName,
__in LPCTSTR lpKeyName,
__in LPCTSTR lpDefault,
__out LPTSTR lpReturnedString,
__in DWORD nSize,
__in LPCTSTR lpFileName
);
|
lpAppName = Section Header
lpKeyName = Variable
lpDefault = if there was an error, it will return the character you set it to
lpReturnedString = returns the variable value
nSize = size in characters
lpFileName = location and name of INI
Here is an example of using both of them
| Code: |
#include <iostream>
using std::cout;
using std::endl;
#include <windows.h>
int main()
{
char cDir[MAX_PATH] = { 0 };
GetCurrentDirectory(MAX_PATH, cDir);
lstrcat(cDir, "\\Test.ini");
if (WritePrivateProfileString("Something", "Value", "something", cDir))
{
cout << "INI was created at " << cDir << endl;
char value[225] = { 0 };
GetPrivateProfileString("Something", "Value", "", value, 225, cDir);
cout << "The key value is " << value << endl;
}
else
cout << "INI was not created" << endl;
return 0;
}
|
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon Dec 10, 2007 4:31 am Post subject: |
|
|
| Code: | #include <iostream>
#include <fstream>
int main(int argc, char** argv)
{
std::ofstream iniOut("inifile.ini");
if(iniOut.is_open()) {
iniOut << "Something=7" << std::endl;
iniOut.close();
std::cout << "Ini file written successfully." << std::endl;
}
else
std::cout << "Problems w/ opening the file." << std::endl;
return EXIT_SUCCESS;
} | No need for external libraries.
|
|
| Back to top |
|
 |
slippppppppp Grandmaster Cheater
Reputation: 0
Joined: 08 Aug 2006 Posts: 929
|
Posted: Mon Dec 10, 2007 11:43 am Post subject: |
|
|
| Ok, i decided to use GetPrivateProfileInt, but how do i make it read a custom value?. Ie, ive seen examples of it seeing if the value is 100 , but i want it to retrieve a value from the ini and set it as an integer.
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Dec 10, 2007 3:05 pm Post subject: |
|
|
atoi will convert a string to an integer.
| Code: |
char cSomething[] = "100";
int iSomething = atoi(cSomething);
|
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Dec 11, 2007 5:48 am Post subject: |
|
|
atoi is bad for your health.
| Code: | #include <iostream>
#include <string>
#include <sstream>
int main(int argc, char** argv)
{
std::string sNumber("123");
int iNumber;
std::istringstream iss(sNumber);
if( iss >> iNumber )
std::cout << "iNumber == " << iNumber << std::endl;
else
std::cout << "Something weird happened." << std::endl;
return EXIT_SUCCESS;
} |
| Code: | #include <iostream>
#include <boost/lexical_cast.hpp>
int main(int argc, char** argv)
{
std::string sNumber("123");
int iNumber;
try {
iNumber = boost::lexical_cast<int>(sNumber);
std::cout << "iNumber == " << iNumber << std::endl;
}
catch(boost::bad_lexical_cast &) {
std::cout << "Something weird happened." << std::endl;
}
return EXIT_SUCCESS;
} |
|
|
| Back to top |
|
 |
|