• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

I want to read a ini file ....!

I want to read a Test.ini file. That look likes this:
[1]
FROM='L:\X\system'
TO='C:\Test'
[2]
FROM='L:\Y\DLL'
TO='C:\Test'
.....
I have tried to use:
char szBuf[80];
GetPrivateProfileString(0, 0, "Kalle",                                  szBuf, sizeof(szBuf), FileName);
MessageBox( szBuf, "GetPrivateProfileString", MB_OK);
The szBuf result is '1'. How do get the the rest 2, 3, 4 ... and how can I get the FROM='L:\X\system' and TO='C:\Test' for every 2,3,4 ... ??
Thanks!!

0
bert1
Asked:
bert1
  • 3
1 Solution
 
cpopinCommented:
Try:

DWORD e = GetPrivateProfileString("1","FROM","(none)",szBuf, sizeof(szBuf) - 1,"Test.ini");
 

0
 
cpopinCommented:
You'll have to loop for for each of the other strings...let me make you an example...

Do you have a preference to using or not using STL?
0
 
cpopinCommented:
Here is a demostration of populating an STL vector template of strings with the contents of your INI file and displaying them.  It has been written as a console app to make output easier and to supply a complete working program, but all of the important code can be used in a Windows application.

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;

#include <windows.h>

// Note: Using const here as to make class unmodifiable.
// const can be removed otherwise.
class fromTo {
public:
    fromTo(const char *szFrom, const char *szTo)
        : m_From(szFrom), m_To(szTo) { }
    const string& GetFrom() const { return m_From; }
    const string& GetTo() const { return m_To; }

private:
    string m_From;
    string m_To;
};

typedef vector<fromTo> vFT_t;
typedef vFT_t::iterator itrFT_t;
typedef vFT_t::const_iterator citrFT_t;

void main()
{
    int iCount = 0;
    vFT_t vsFromTo;
    // Note: The .\\ prefixing the Test.ini name says look for it in
    // the current directory.  Remove it and it'll use the Windows directory.
    const char sTestIni[] = ".\\Test.ini";

    while (1) {
        char szNum[10], szFrom[1024], szTo[1024];

        ++iCount;
        itoa(iCount, szNum, 10);    // Convert int to string
        GetPrivateProfileString(szNum,"FROM","",szFrom, sizeof(szFrom) - 1,
            sTestIni);
        if (!strlen(szFrom))
            break;
        GetPrivateProfileString(szNum,"TO","",szTo, sizeof(szTo) - 1,
            sTestIni);
        if (!strlen(szTo))
            break;
        {
            fromTo temp(szFrom, szTo);
            vsFromTo.push_back(temp);
        }
    }

    // For loop demonstrating the use of an iterator, a better way to
    // walk through STL type containers.
    int i = 1;
    for (citrFT_t p = vsFromTo.begin(); p != vsFromTo.end(); ++p, ++i)
        cout << i << ": From: " << p->GetFrom()
            << ", To: " << p->GetTo() << endl;

    cout << endl;

    // For loop demonstrating the more traditional method of walking
    // an array using an int index.  Note this is only available to
    // certain types of STL containers: vector, map, etc.
    for (int j = 0; j < vsFromTo.size(); ++j)
        cout << j+1 << ": From: " << vsFromTo[j].GetFrom()
            << ", To: " << vsFromTo[j].GetTo() << endl;

    cout << "\nPress RETURN to exit: ";
    cin.get();
}
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now