Jump to content
Sign in to follow this  
mgh

Reading an Ini File in C (MinGW)... doesn't appear to work in VS 2005

Recommended Posts

If you want to read an INI file, here's how.Note: This works for me using the MinGw © compiler.The same code when compiled through VS 2005 C++ is another story (it'll compile, but I'll get warnings and it'll return as a string 'data not found' when I run the gauge in FS9/FSX as per the code). Why? Dunno, still looking into it. // Read INI File Code in a C gauge. Display pilot's name as a string.char buffer1[100];char FILEPATH[300];{ GetCurrentDirectory(300, FILEPATH);strcat(FILEPATH,"FS2CrewStart CenterA320F.ini"); // Path to INI fileGetPrivateProfileString("FS2Crew Data","PILOT","Data not found",&buffer1,100,FILEPATH); }The INI file (named A320F.ini) that the code above would read, for example, would look like this:-FS2Crew Data- // Use squarebrackets here..Avsim forum formatting!PILOT=Captain XIn your gauge, you could now display "Captain X" as a string.sprintf(pelement->string,"%s",buffer1);-Bryan

Share this post


Link to post
Share on other sites
Guest Patrick_Waugh

Much better to build a class that will read (and write) whatever you want to any .ini, that way you don't have to re-invent the wheel each time.Also, you should look up the warnings on MSDN2 so that you can learn the new functions to use to both avoid the warnings, and to discover the more secure versions that have replaced them.

Share this post


Link to post
Share on other sites

The original poster specifically refers to the C language. Classes don't exist in C.EDIT"&buffer" should surely be replaced by "buffer"? The name of an array is a pointer to the first item in the array and the relevant argument in GetPrivateProfileString is:LPTSTR lpReturnedString, // points to destination buffer

Share this post


Link to post
Share on other sites

As an off-topic follow-up, GetPrivateProfileString shows a subtle trap for the programmer on changing operating systems.http://msdn2.microsoft.com/en-us/library/ms724353.aspxIf the key can't be found then the DefaultString is copied to the ReturnedString buffer and any trailing blanks removed.In Windows Me/98/95 the trailing blanks are also removed from the DefaultString (even though it's declared as a constant parameter!) This does't happen with other operating systems - such as XP and Vista.According, a programmer who tested if the key was found by comparing the ReturnedString with the DefaultString would get different results if DefaultString contained trailing blanks and the key wasn't found. The ReturnedString would equal the DefaultString - ie key not found - using Me/98/95 but it wouldn't using XP/Vista!I wonder what other similar traps there are on changing from XP to Vista?

Share this post


Link to post
Share on other sites
Guest Patrick_Waugh

As I have helped him in the past, I know he can compile in C++ should he desire, despite the what you have assumed from his post.

Share this post


Link to post
Share on other sites
Guest Patrick_Waugh

If you are still developing for Win98 or ME, you need more than programming help. :D

Share this post


Link to post
Share on other sites

For those following this thread, I resolved the issue.Stupidest thing, in VS 2005 I had the Character Set in the Properties set to Unicode instead of Multi Byte.So now you know where to look if you run into the same thing :-)Cheers,Bryan

Share this post


Link to post
Share on other sites

1 - I assumed nothing. The original poster specifically refered to using the MinGw © compiler for his code. Anyway, why bother with a creating an inappropriate class in C++? The original poster's code is tight and efficient.2 - Do try to understand posts before rushing to reply. I was pointing out that code correctly written for Windows Me/98/95 could give an error when run under later operating systems and speculated if there were any similar changes between XP and Vista. Anyway, my understanding is that FS9 contains legacy code which may have been written pre-XP.

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  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...