Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1999-07-01
3
Medium Priority
?
218 Views
Last Modified: 2010-04-02
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
Comment
Question by:bert1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 2

Accepted Solution

by:
cpopin earned 120 total points
ID: 1198893
Try:

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

0
 
LVL 2

Expert Comment

by:cpopin
ID: 1198894
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
 
LVL 2

Expert Comment

by:cpopin
ID: 1198895
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question