| View previous topic :: View next topic |
| Author |
Message |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Jan 21, 2008 12:12 pm Post subject: [C++]Reading data from .ini |
|
|
OK, I have a .ini file called Settings.ini and in it saids
| Code: |
[Positions]
X = 100
Y = 200
|
And I try to use this code
| Code: |
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
UINT X;
UINT Y;
X = GetPrivateProfileInt("Positions","X",1000,"Settings.ini");
cout << "X is " << X;
system("pause");
return 0;
}
|
I keep getting this error. Can someone please help.
| Code: | .\test.cpp(9) : error C2664: 'GetPrivateProfileIntW' : cannot convert parameter 1 from 'const char [10]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast |
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Mon Jan 21, 2008 12:40 pm Post subject: |
|
|
| X = GetPrivateProfileInt(L"Positions",L"X",1000,L"Settings.ini");
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Mon Jan 21, 2008 12:41 pm Post subject: |
|
|
Don't use Unicode. If you plan too, typecast.
Otherwise, just change the project settings to Multi-byte.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jan 21, 2008 1:21 pm Post subject: |
|
|
You have your character set in the compiler options to unicode. If you are going to continue using unicode you can either use the TEXT( ) macro, or use the L macro for your strings:
So instead of:
"Positions"
Use:
TEXT("Positions")
Or
L"Positions"
_________________
- Retired. |
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Jan 21, 2008 2:00 pm Post subject: |
|
|
Thanks appalsap, adding L in front of everything does anke it work. But for some reason, it keeps saying X is 1000. That means either it can't find X or read it in the .ini. I've put the Settings.ini in the same folder as the program, but it still says X is 1000. What am I doing wrong?
I have a feeling it is going to be a very simple answer that I don't know...
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 21, 2008 3:13 pm Post subject: |
|
|
lpFileName
The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
You need to give a full file path. Put the settings.ini in Windows directory and see if it works...
"Note This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry."
_________________
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Mon Jan 21, 2008 3:22 pm Post subject: |
|
|
| blankrider wrote: | | "Note This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry." |
Why are INI files deprecated in favor of the registry?
|
|
| Back to top |
|
 |
mer0x Advanced Cheater
Reputation: 0
Joined: 06 Jan 2008 Posts: 63
|
Posted: Mon Jan 21, 2008 5:38 pm Post subject: |
|
|
| Code: |
DWORD ReadKey( LPCTSTR szSection, LPCSTR szKey, LPTSTR szReturnString ){
LPCTSTR APPNAME = "\resources.ini";
GetPrivateProfileString(szSection,szKey,0,szReturnString,255, APPNAME);
return true;
}
|
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Jan 21, 2008 8:58 pm Post subject: |
|
|
I don't want to type (below)
| Code: | | C:\\Documents and Settings\\Desktop\\Other stuff\\Blah\\Blah\\Blah |
over and over again.
Can someone teach me how to use this code (below)
| Code: | DWORD WINAPI GetCurrentDirectory(
__in DWORD nBufferLength,
__out LPTSTR lpBuffer
); |
Can someone give me an example of it, I have no idea what this is saying right now.
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 21, 2008 9:18 pm Post subject: |
|
|
LPTSTR cOutputFolder = NULL;
if(GetCurrentDirectory(sizeof(cOutputFolder), cOutputFolder) = 0)
//error
else{
//the cOutputFolder has the string of the folder
}
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Jan 22, 2008 6:04 am Post subject: |
|
|
| blankrider wrote: | LPTSTR cOutputFolder = NULL;
if(GetCurrentDirectory(sizeof(cOutputFolder), cOutputFolder) = 0)
//error
else{
//the cOutputFolder has the string of the folder
} |
Couple of errors with your code, you are trying to set GetCurrentDirectory to 0 instead of comparing it. You forgot the first set of braces { } for the if statement as well. Along with that you are using sizeof() on an undefined size array. The sizeof() in this case will return 4 because LPTSTR is a pointer to a string. Instead, you should write it like:
| Code: | TCHAR tszCurrentDirectory[MAX_PATH] = {0};
if( GetCurrentDirectory( sizeof(tszCurrentDirectory), tszCurrentDirectory ) == 0 ){
// Success
}else{
// Error
} |
_________________
- Retired. |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Tue Jan 22, 2008 6:08 am Post subject: |
|
|
and since the first parameter of GetCurrentDirectory is the length of the buffer of the current directory in TCHARs it should be
| Code: |
(sizeof(tszCurrentDirectory)/sizeof(tszCurrentDirectory[0]))
|
Last edited by appalsap on Tue Jan 22, 2008 6:29 am; edited 1 time in total |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Jan 22, 2008 6:18 am Post subject: |
|
|
| appalsap wrote: | and since the first parameter of [i]GetCurrentDirectory[i] is the length of the buffer of the current directory in TCHARs it should be
| Code: |
(sizeof(tszCurrentDirectory)/sizeof(tszCurrentDirectory[0]))
|
|
Whoops forgot to change that. Alternatively you could also use the _ARRAYSIZE macro for that.
_________________
- Retired. |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Jan 22, 2008 6:33 am Post subject: |
|
|
Wiccan in C++ you odnt need the {} after a 1 line if
if(A!=B)
MessageBox(0, "Error", "Error", MB_OK);
else
//some other 1 line thing
that would run, you only need {} with multi line conditionals
_________________
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Tue Jan 22, 2008 6:40 am Post subject: |
|
|
| blankrider wrote: | Wiccan in C++ you odnt need the {} after a 1 line if
if(A!=B)
MessageBox(0, "Error", "Error", MB_OK);
else
//some other 1 line thing
that would run, you only need {} with multi line conditionals | Yes, but if you check Wiccaan's code, he allows the user to input their own code in between the brackets, which could be more than one line of code.
|
|
| Back to top |
|
 |
|