Jump to content
  • Advertisement
Sign in to follow this  
ARC inc

GetPrivateProfileString

This topic is 3155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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?

Share this post


Link to post
Share on other sites
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!