Original Post
I have a settings file I want my application to read and then use those settings to setup the application. The file looks something like this: [GENERAL] Width = 800 Height = 600 Windowed = 1 ColourDepth = 32 [ADVANCED] BBufferFormat = A8R8G8B8 DepthStencilBufferFormat = D24S8 Multisample = 0 Not too complicated. I have attempted writing a parser for it but it's not going too well. I read each line into a buffer. If it starts with a '[' then I skip it because it's a sort of comment line. If it's not a comment then I read everything before the '=' into a key buffer and everything after into a value buffer. This method only half works. I got my app to print the outputs of the keys and values it read from the files and I get something like this: Width800 Height800 Windowed8600 ColourDepth1 BBufferFormatDepthStencilBufferFormat Multisample1BufferFormat Multisample1Bu0 Which is ...uhhh... not right. So I was wondering if anyone could help me work out what might be going on. The code for parsing the file is below: (This is my first attempt at writing a file parser so it's probably rubbish :P)
while(fgets(chLineBuffer, sizeof(chLineBuffer), pSettingsFile) != NULL) { //printf("%s", chLineBuffer); //It's a section header so skip it if(strstr(chLineBuffer, "[GENERAL]") != NULL) continue; else if(strstr(chLineBuffer, "[ADVANCED]") != NULL) continue; //Otherwise read everything into the right buffer for(int i = 0; i < sizeof(chLineBuffer); i++) { if(chLineBuffer != '=' && chLineBuffer != ' ') { if(bReadKey) { chKeyBuffer = chLineBuffer; printf("%s", chKeyBuffer); } else if(bReadValue) { chValueBuffer = chLineBuffer; } } else if(chLineBuffer == '=') { if(bReadKey) { bReadKey = false; bReadValue = true; printf("%s", chKeyBuffer); } printf("%s", chValueBuffer); continue; } }