7

This is a very simple thing, so I want to keep it as simple as it sounds. All I want is to load a bunch of key-value paires from a file, and populate them in to a map. I do not really care how the text is structured, as long as it is easy to read.

What i have now is:

  • xml with xsd generated code (overkill)
  • Protocol buffer (also overkill)
  • INI style text file

I like the syntax of the INI file, but I not want to write a parser for that. It sounds to me like I would be doing something lots of people have done before me. Is there not some sort of library to read simple structured files like this?

W. Goeman
  • 1,674
  • 2
  • 15
  • 31
  • 2
    csv format is even simpler, each line is a pair of values like: key;value. So your key/values are separated by semicolon. I doubt you will find any libraries for that (but some may exists) since parsing it is so simple. – marcinj Jun 08 '12 at 15:14
  • 1
    possible duplicate of [CSV parser in C++](http://stackoverflow.com/questions/1120140/csv-parser-in-c) – gbjbaanb Jun 08 '12 at 15:18
  • @gbjbaanb, you are right, csv should work for me as well. However, I would still be interested in seeing a parser for the standard INI format. – W. Goeman Jun 08 '12 at 15:22
  • If you are on windows then you can use the GetPrivateProfile... functions to get values from .ini files. Very easy to do. Easier than CSV (which should use commas not semi-colons!) even. On linux you would need a library though – Dennis Jun 08 '12 at 15:22
  • @Dennis, sounds perfect, but I am on linux :) – W. Goeman Jun 08 '12 at 15:25
  • 1
    http://stackoverflow.com/questions/2280352/use-ini-file-in-c-on-linux – Dennis Jun 08 '12 at 15:25

2 Answers2

8

Since you seem to want the simplest thing humanly possible, I'm going to suggest something incredibly simple that may or may not work based on your map contents. If your map data values are strings that include spaces, this wont work. If they're strings without spaces, or numeric, you're set.

This isn't tested code, but it's close and simple so you should be fine even if it doesn't quite compile. Just change KeyType and ValueType to int, string, float, or whatever you're actually using in the file.

Set up file like:

key value
key2 value2
key3 value3
key4 value4

Read like:

KeyType key;
ValueType value;

std::map<KeyType, ValueType> myMap;
while (infile >> key >> value)
    myMap[key] = value;
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • 2
    That is very simple indeed! Would work perfectly for what I am doing. Still, not INI format, but should work – W. Goeman Jun 08 '12 at 15:36
  • 4
    -1, the loop condition should be `while (infile >> key >> value)` and for the loop body `myMap[key] = value;` is simpler – Jonathan Wakely Jun 08 '12 at 15:48
  • 1
    Basic idea's great, but just to emphasise and explain what Jonathan said: this is not simply a matter of style - the `eof()` condition is not set until after a failed attempt to read past the end (thought experiment: even if not at the last character in `infile`, how could it know at the `while()` whether it had enough characters for both key and value?). Separately, and FWIW, this can work for `std::string` keys and values with spaces with a simply change to `while (getline(infile, key) && getline(infile, value))`. – Tony Delroy Jun 08 '12 at 16:02
  • 1
    Agreed, didn't think it through far enough - thanks for the suggestion, have modified answer. And I like Tony's idea if you're actually going to use this for multiword string values. – John Humphreys Jun 08 '12 at 17:29
0

If you are in the MS world you can use

GetPrivateProfileSectionNames
GetPrivateProfileString
WritePrivateProfileString

to read from ini file or regestry. If you want to write Unicode make sure a newly created file gets the BOM of UTF16.

Totonga
  • 4,236
  • 2
  • 25
  • 31