GetPrivateProfileString

Started by
3 comments, last by rip-off 14 years, 3 months ago
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]
Advertisement
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?
the pointer is initialized as you can see

servConfig a*; which would be making a the pointer to servConfig

unless I am lost on what your saying.
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.
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.

This topic is closed to new replies.

Advertisement