Question


Assume that I have this ini file:

[Ranks]

John    = 0
David   = 0
Albert  = 3
Tommy   = 1
Cyrus   = 3
Adam    = 0
Sarah   = 1
Alecia  = 3

I want to count how much 0 ,1 and 3 I have. It's easy when I have the names and check their values then count them, but how's this possible to count them when I don't have the names

I prefer to use just some simple WINAPI's rather than using any class or library
PS: I'm using MFC

Was it helpful?

Solution

I don't think there is any predefined function for such a rather specialized task.

And I don't see what difference it really makes whether you have the names of the entries to look for.

The basic algorithm is however really easy:

  1. Create a std::map<int, int>, containing as key each value you are interested in, and as value a 0 (that will be the count of such values in the end)
  2. Read each line of the file
  3. If the line doesn't start with a [ and contains a =
    1. extract the part between the = and line end.
    2. find the entry with the extracted part as key in the map
    3. increase the count for that key
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top