Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm facing an issue with reading values from an INI file. Despite using the GetPrivateProfileString function, it keeps returning a blank value. Could you please assist me in identifying the problem in my code?
#include <Windows.h>
#include <iostream>
#include <string>

int ini_Read(const wchar_t* Paths_ini, const wchar_t* Section, const wchar_t* Key) {
    const int bufferSize = 256; // Size of the buffer for the string
    wchar_t buffer[bufferSize];   // Buffer to store the read string

    GetPrivateProfileString(Section, Key, NULL, buffer, bufferSize, Paths_ini);
    std::wstring userNumMax(buffer);

    // Print the value of the key
    std::wcout << L"Value read from the key '" << Key << L"': " << userNumMax << std::endl;

    return 0;
}

int main() {
    // Example call to the ini_Read function
    const wchar_t* pathsIni = L"C:\\Users\\pct\\source\\repos\\ConsoleApplication1\\ConsoleApplication1\\correlazioni.ini";
    const wchar_t* section = L"soglia_minima";
    const wchar_t* key = L"symbl2";

    ini_Read(pathsIni, section, key);

    return 0;
}


What I have tried:

The INI file structure is as follows:

[primario]
symbl1=AUDUSD
symbl2=AUDJPY
[correlato]
symbl1=NZDUSD
symbl2=NZDJPY
[soglia_minima]
symbl1=0.4
symbl2=0.4

I'm looking to retrieve the values in the [soglia_minima] section, particularly the symbl3 key, but it returns an empty string. Can you help me identify what might be causing this issue?

Thank you in advance for your assistance!
Posted
Updated 25-Jul-23 4:01am
Comments
[no name] 25-Jul-23 9:37am    
First thing that comes to mind is that your INI file could be ASCII text. You can use notepad to resave it as Unicode or modify your code to use GetPrivateProfileStringA

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestringa
Richard MacCutchan 25-Jul-23 9:45am    
I tried that also, and it works fine whether the .ini is ASCII or Unicode.

I just tried your code and it initially failed because my path to the .ini file was not correct. Once I fixed that it worked fine. You can check for yourself by modifying your code as follows:
C++
int num = GetPrivateProfileString(Section, Key, NULL, buffer, bufferSize, Paths_ini);
if (num == 0)
{
    DWORD dw = GetLastError();
    std::cout << "Error: " << dw << std::endl;
}

If the error number is 2, then your path to the .ini file is not correct. See GetPrivateProfileString function (winbase.h) - Win32 apps | Microsoft Learn[^] for full details.
 
Share this answer
 
Comments
Rick York 25-Jul-23 11:27am    
That code looks appropriate to me.
Richard MacCutchan 25-Jul-23 11:55am    
Thanks, it seems to work OK.
thanks i change in utf file and workit also with
GetPrivateProfileStringA
thanks
 
Share this answer
 
Comments
Richard MacCutchan 25-Jul-23 10:47am    
That will not work with your code above, since all the parameter values are Unicode. I have run it with GetPrivateProfileStringW, and it correctly reads text that is in ASCII or Unicode.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900