Download std :: map from a text file

This is a very simple thing, so I want it to be as simple as it seems. All I want to do is load a bunch of key-value pairs from a file and fill them on the map. I donโ€™t care how the text is structured if it is easy to read.

What I have now:

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

I like the syntax of the INI file, but I do not want to write a parser for it. It sounds to me as if I were doing something that many people did. Is there any library for reading simple structured files like this?

+6
source share
2 answers

Since you seem to want the simplest thing humanly possible, Iโ€™m going to offer something incredibly simple that may or may not work based on your map contents. If your map data values โ€‹โ€‹are strings containing spaces, this will not work. If they are strings without spaces or numeric, you are configured.

This is not verified code, but it is 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 are actually using in the file.

Configure the file as:

key value key2 value2 key3 value3 key4 value4 

Read how:

 KeyType key; ValueType value; std::map<KeyType, ValueType> myMap; while (infile >> key >> value) myMap[key] = value; 
+7
source

If you are in the MS world, you can use

 GetPrivateProfileSectionNames GetPrivateProfileString WritePrivateProfileString 

to read from an ini file or registry. If you want to write Unicode, make sure that the newly created file receives the UTF16 specification.

0
source

Source: https://habr.com/ru/post/917642/


All Articles