CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    28

    Help reading ini file

    Hello,

    I am trying to read a ini file but having some problems.

    Suppose I have a ini file, "infoDat.ini". It contains:

    Code:
    [Guy]
    name=Magni
    id=5
    [Girl]
    name=Sarah
    id=6
    Now I am trying to read "id" key from section "Guy", here's what I did:

    Code:
    int getID;
    
    getID = GetPrivateProfileInt("Guy", "id", -1, "infoDat.ini");
    Now the problem is, the function always return -1. Why is that? Isn't it suppose to return 5?

    Please help me!!!

  2. #2
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Help reading ini file

    You get your default value specified in the 3rd parameter in the call to GetPrivateProfileInt(). Most certainly the "infoDat.ini" file is not properly formatted, or it cannot be located by a relative path. To remedy specify a full file path in the lpFileName parameter.

    PS. You realize that GetPrivateProfileInt() API was deprecated in favor of the System Registry APIs, don't you?

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Help reading ini file

    From MSDN
    lpFileName [in]

    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.
    Best regards,
    Igor

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Help reading ini file

    The cure:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    #define COUNTOF(x)  (sizeof(x)/sizeof(x[0]))
    
    BOOL MakeFilePathNextToMe(LPCTSTR fileName, LPTSTR outPath, DWORD cchPath)
    {
    	if (GetModuleFileName(NULL, outPath, cchPath))
    	{
    		LPTSTR p = _tcsrchr(outPath, TEXT('\\'));
    		if (p)
    		{
    			_tcscpy(p + 1, fileName);
    			return TRUE;
    		}
    	}
    	return FALSE;
    }
    
    int _tmain()
    {
    	int getID;
    
    	TCHAR iniPath[MAX_PATH] = {0};
    
    	if (!MakeFilePathNextToMe(TEXT("infoDat.ini"), iniPath, COUNTOF(iniPath)))
    	{
    		return -1;
    	}
    
    	getID = GetPrivateProfileInt(TEXT("Guy"), TEXT("id"), -1, iniPath);
    	_tprintf(TEXT("id = &#37;d\n"), getID);
    
    	return 0;
    }
    Best regards,
    Igor

  5. #5
    Join Date
    Mar 2010
    Posts
    28

    Re: Help reading ini file

    I did try and specify the full path but didn't work. And I also know GetPrivateProfileInt() API was deprecated in favor of the System Registry APIs, but in this situation, I have to use ini file.

    I will try Igor's code and see what happens.

  6. #6
    Join Date
    Mar 2010
    Posts
    28

    Re: Help reading ini file

    Nope, Igor's code is also giving "id = -1". lol why is this happening?

  7. #7
    Join Date
    Mar 2010
    Posts
    28

    Re: Help reading ini file

    Ok, got it. Finally it worked. I needed to keep the ini file in Debug folder. lol

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Help reading ini file

    You need to keep .ini file next to your executable wherever it is, Debug folder in case you run Debug build from VS IDE. I thought it'd easy to get from the source.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured