Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Can't modify config file

This thread is locked; no one can reply to it. rss feed Print
Can't modify config file
Emily Brace
Member #16,568
October 2016

Hello!

I have some code to check if there's a config file and if there isn't, it will create one, simple stuff. I can then read from the file just fine, but when I try to set values and then save, the save fails.

Here's some of my code.
I first load the config file in the beginning of my code

// Outside of main()
ALLEGRO_CONFIG* config;
// Inside of main()
config = al_load_config_file("config.ini");
// This works just fine
value = al_get_config_value(config, "blah" "blah");
// But this doesn't :c
al_set_config_value(config, "blah", "blah", "blah");
// This returns false and the file isn't changed
al_save_config_file("config.ini", config);

Not sure what I'm doing wrong.
Thanks! :D

Meow :3

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Hey Emily,

Here are a few things to try.

1. Is the file read-only? Save would fail in that case.

2. Have you compiled in debug mode or release mode with debugging info and checked allegro.log for clues?

3. Have you tried saving as a different file?

4. How do you know al_set_config_value has failed? Have you checked it with al_get_config_value?

5. What version of Allegro 5 are you using? If it's a bug, it may have been fixed by now.

Emily Brace
Member #16,568
October 2016

1.
No, it can create the file just fine.

2.
Tried compiling in debug mode. Where can I find allegro.log?

3.
Yes, and that also fails.

4.
Sorry that I was unclear. Meant that al_save_config_file failed. If I try to check the value before and after, the value changes.

5.
Running Allegro 5.2.1.1

Meow :3

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

al_save_config_file only fails in a couple of cases. One, where the files fails to be created, to be saved, or to be closed. Two, where there is an error in the file stream.

allegro.log will be created in the same folder as your program if the version of allegro you linked to has debugging logs enabled.

Your best bet is to debug this yourself. Set a breakpoint in config.c:433, and then check the return values of retsave and retclose and see if there was an error. If the breakpoint isn't triggered, then the file couldn't be created. If retsave is false, then set a breakpoint in al_save_config_file_f and step through the function checking return values.

I can give you a short tutorial on debugging with gdb if you need help.

Can you provide a small test case that fails for you? And provide the config file and source file you're using? That way I can test it out on my machine and see if it works for me or not.

Emily Brace
Member #16,568
October 2016

I tried recompiling Allegro with WANT_RELEASE_LOGGING, but no log is being created.
I'll see what I can figure out...

Just to be clear, I'm trying to update an already created config file. A workaround could be to remove the file and create it again if it's needed...

EDIT: Tried removing the file and write it again like this and it removes the file, but doesn't write it again

ifstream fileCheck("config.ini");
if (fileCheck.good())
{
  fileCheck.close();
  remove("engine.ini");

  ALLEGRO_CONFIG* config = al_create_config();
  al_set_config_value(config, "Blah", "Bloh", "Bleh");
  al_set_config_value(config, "Blah", "Bleh", "Bloh");

  al_save_config_file("engine.ini", config);
}

Meow :3

SiegeLord
Member #7,827
October 2006
avatar

You're calling this after al_init, right?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Emily Brace
Member #16,568
October 2016

Yes, I can create it just fine, just not modify it afterwards.
I'm using various other Allegro stuff for graphics etc. so I know it's working.
Dunno if PhysFS is messing stuff up though.

EDIT: Yup, after initializing PhysFS, you can't save config files, since PhysFS doesn't support writing to zip files. Not sure if bug or not though.

Meow :3

SiegeLord
Member #7,827
October 2006
avatar

You can probably configure PhysFS to be able to save outside the zip file if you mount a real directory. Alternatively, you could call al_set_standard_file_interface() before saving your config file and then restoring it with al_set_physfs_file_interface().

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Emily Brace
Member #16,568
October 2016

Well, that would also be cool, but the point was never to write to the zip file itself. The issue was just that it tried to do that and that was why it failed.

Meow :3

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

That's why it's always best to try and provide a minimum, complete, verifiable example (MCVE like Stack Overflow likes to call it), so others can try to reproduce it and so we can see everything that's going on. I would never have guessed you were using PhysFS from what you showed us. ;)

Emily Brace
Member #16,568
October 2016

Good point, although, I wouldn't have thought of including that code either way xd

Meow :3

Elias
Member #358
May 2000

I wonder if we should provide a function al_set_physfs_file_interface_read_only. But then when you try and read your config file it still would try to read from the .zip - I guess it has to be the user's responsibility to set the file interface they want to use.

--
"Either help out or stop whining" - Evert

Emily Brace
Member #16,568
October 2016

Or some version of the function that excludes config files :p

Meow :3

Go to: