-1

I have a file 'config.ini' and I would store some settings info in it.

For example:

##CONFIG.INI##
#window-resolution = 1920x1080
#player-movement = 100
#appstate = state_menu

Now how I can read data after the '=' from my file to my variables? I think I have to use std::getline, but I don't know how.

Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
szym3ns
  • 5
  • 3
  • This should be of use: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Jan 28 '19 at 17:46
  • Yes getline to read line per line, when scanf to extract word after '=' if present. Don't be afraid to do and show, is simple to do ... – bruno Jan 28 '19 at 17:48

1 Answers1

1

how I can read data after '=' from my file to my variables?

Learn more about parsing techniques, notably about recursive descent parsers.

Indeed, you should read the entire line (e.g. with std::getline, or perhaps even readline(3) on Linux, if the configuration is interactive) then use appropriate parsing techniques on the line you have got.

Consider reading the first half of the Dragon Book. It should help you a lot.

You could also find some free software libraries (like libconfig and many others) to parse configuration files. See e.g. this related question.

You could also use parser generators like GNU bison (to be perhaps used with flex) or ANTLR. They would generate for you some C or C++ code, once you provided them some annotated grammar describing your input language. For documentation purposes at least, you want to describe that input language (something explaining the expected contents of your config.ini) in EBNF notation.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547