Sign in to follow this  
ARC inc

GetPrivateProfileString

Recommended Posts

Ok so I am using GetPrivateProfileString for a server config file an it seems to always returns 0 no matter what my ip is... config.h
class servConfig
{
    public:
        int  GetServerIP();
        int GetserverPort();

};

int servConfig::GetServerIP()
{
   _TCHAR server[1000];

  GetPrivateProfileString("Server", "server", "127.0.0.1", server, 1000, "./data/config.ini");
  
}
int servConfig::GetserverPort()
{
   _TCHAR serverport[1000];
   GetPrivateProfileString("Server","port","127.0.0.1", serverport, 1000, "./data/config.ini");
   
}

main.cpp
#include "config.h"
#include <windows.h>

int main()
{
   serverConfig *a;

   printf("%u\n", a->GetServerIP());
   return 0;
}
why does it keep on not printing my IP address I'd like to use for the server :( config.ini
[Server]
server = 127.0.0.1
port   = 6000

any ideas on why it would not print the server IP?? [/source]

Share this post


Link to post
Share on other sites
You appear to be calling the function on an uninitialised pointer. This is undefined behaviour, which explains any behaviour you might be seeing. Also, have you tried using the return value of the function to discover if an error occurred?

Share this post


Link to post
Share on other sites
I think rip_off meant that your servConfig object is not initialized. In main, you have serverConfig * a, but that is an uninitialized pointer. You need something like serverConfig * a = new serverConfig(); or simply stack allocate it as serverConfig a.

Share this post


Link to post
Share on other sites
My point was that you any complications you add to your program make it harder for us to diagnose the root cause. Here, your code contains an additional bug, which isn't present in the following, minimal test case:

#include <iostream>
#include <windows.h>


int main()
{
_TCHAR server[1000] = {};

GetPrivateProfileString("Server", "server", "127.0.0.1", server, 1000, "./data/config.ini");


std::cout << server << '\n';
}



Your actual problem is that your functions don't return anything. This is your program relying on more undefined behaviour. You should probably increase your warning level, and make it treat warnings as errors, so that it rejects such programs. You may have an unrelated issue if your file is missing, misnamed or in the wrong directory.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this