-2

So I've decided to take on a new hobby as i cant afford to build PCs, anyway so far Im trying to create a simple console based program that reads information from an ini file however it doesn't seem to read it at all and instead just outputs the default value. and random intergers.

char* ReadINI(const char* sSection, const char* sSub, const char* sDefaultValue)
{
    char* sResult = new char[255];
    memset(szResult, 0x00, 255);
    GetPrivateProfileString(  sSection, sSub, sDefaultValue, sResult, 255, ".\\config.ini");
    return sResult;
}

int s_width = (int)ReadINI("CONFIGURATION", "WIN_WIDTH", 0);
int s_height = (int)ReadINI("CONFIGURATION", "WIN_HEIGHT", 0);
const char* value = ReadINI("CONFIGURATION", "WIN_NAME", "null");

This is my output as a way of debug and so the user knows the app has read there settings correctly.

std::cout << "Width: " << s_width << "\n"; // Displays random integer
std::cout << "Height: " << s_height << "\n"; // Displays random integer
std::cout << "Name: " << value << "\n "; // Displays null

This is my .ini file

[CONFIGURATION]
WIN_NAME="Notepad"
WIN_WIDTH="800"
WIN_HEIGHT="600"
3
  • I didn't realize GetPrivateProfileString was still supported. Wowzers! Doesn't anything ever really go away in Windows? Jun 20, 2019 at 6:20
  • int s_width = (int)ReadINI("CONFIGURATION", "WIN_WIDTH", 0); is not right. You perhaps wanted to use atoi Jun 20, 2019 at 6:21
  • @user4581301 that's one of the things that makes Windows successful: Reliabiliy that old code won't break. Jun 20, 2019 at 6:37

2 Answers 2

6

Do not pass a relative path to (Get|Write)PrivateProfile... functions, you need to pass an absolute path instead. The PrivateProfile API interprets relative paths as being relative to the Windows installation folder, not to the calling process's current working directory, like you are expecting. This is documented behavior:

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 are not actually reading from your .ini file, so you get your specified default values returned instead.

You can't typecast a char* pointer into an int to get the numeric value you are looking for. You are printing out the memory address that the char* is pointing at, not the integer value that the string represents. You need to parse the string, such as with the Win32 StrToInt(), or the C sscanf(), or the standard C++ library std::atoi() or std::stoi() or std::istringstream. In this situation, a better solution is to use GetPrivateProfileInt() instead and let the API do the parsing for you.

You are also leaking the char* strings that you allocate. You should use std::string instead and let the standard C++ library handle the memory management for you.

With that said, try something more like this:

std::string configFile;

std::string ReadINI_String(const std::string &sSection, const std::string &sSub, const std::string &sDefaultValue)
{
    char sResult[256] = {};
    GetPrivateProfileString( sSection.c_str(), sSub.c_str(), sDefaultValue.c_str(), sResult, 255, configFile.c_str() );
    return sResult;
}

int ReadINI_Int(const std::string &sSection, const std::string &sSub, int iDefaultValue)
{
    return GetPrivateProfileInt( sSection.c_str(), sSub.c_str(), iDefaultValue, configFile.c_str() );
}

...

char path[MAX_PATH] = {};
GetModuleFileNameA(NULL, path, MAX_PATH);
PathRemoveFileSpecA(path);
PathCombineA(path, path, "config.ini");
configFile = path;

...

int i_width = ReadINI_Int("CONFIGURATION", "WIN_WIDTH", 0);
int i_height = ReadINI_Int("CONFIGURATION", "WIN_HEIGHT", 0);
std::string s_value = ReadINI_String("CONFIGURATION", "WIN_NAME", "null");

std::cout << "Width: " << i_width << "\n";
std::cout << "Height: " << i_height << "\n";
std::cout << "Name: " << s_value << "\n";
-2

Variable sResult is in function scope it'll will be in stack As soon as you return from the function it get destroyed.

Allocate memory using malloc or from heap to get the value outside of the function.

This one works, Put the ini in the current working directory and it works.

    char* sResult = new char[255];
    char* ReadINI(const char* sSection, const char* sSub, const char* sDefaultValue)
    {
        memset(sResult, 0x00, 255);
        GetPrivateProfileString(sSection, sSub, sDefaultValue, sResult, 255, ".\\config.ini");
        return sResult;
    }

...

    char pwd[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, pwd);
    MessageBox(NULL, pwd, pwd, 0);
    int s_width = atoi(ReadINI("CONFIGURATION", "WIN_WIDTH", 0));
    int s_height = atoi(ReadINI("CONFIGURATION", "WIN_HEIGHT", 0));
    const char* value = ReadINI("CONFIGURATION", "WIN_NAME", "null");
    std::cout << "Width: " << s_width << "\n"; 
    std::cout << "Height: " << s_height << "\n";
    std::cout << "Name: " << value << "\n "; 
3
  • This is exactly what the author of the post is doing: see line char* sResult = new char[255]; Jun 20, 2019 at 6:41
  • 1
    @Rajaganesh What you say would be true if sResult were declared as a fixed array on the stack, but it is not. It is being dynamically allocated on the heap via new[] instead Jun 20, 2019 at 6:42
  • When I tested it, I found that heap space could output correctly both inside and outside the function. The main problem with OP was that the forced conversion was wrong. Others were good.
    – Strive Sun
    Jun 20, 2019 at 10:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.