Page 1 of 1

Preference fetchString not working..... user error?

Posted: Wed Jul 12, 2017 10:49 am
by chrisvandiemen
I've got some issues with the Preferences library for Arduino.

The following snippet is responsible for storing a variable to EEPROM:

Code: Select all

int DeviceSettings::storeString(const char* key, const char* value){
  preferences.begin(name_space, false);
  Serial.println(preferences.putString(key, value), DEC);
  preferences.end();
}
This outputs '16', which is correct given the input I give it.

When i try to fetch the before stored variable using this code:

Code: Select all

void DeviceSettings::fetchString(const char* key, char* value){
  preferences.begin(name_space, false);
  Serial.println(preferences.getString(key, value, 200), DEC);
  Serial.println(value);
  preferences.end();
}
This outputs '0' and ''

I can't seem to understand what's happening. Both methods receive the same key:

Code: Select all

 settings.storeString(SETTINGS_APP_NAME,"Chris van Diemen");
  char * s;
  settings.fetchString(SETTINGS_APP_NAME, s);
Is this user error of is there something else going on?

Re: Preference fetchString not working..... user error?

Posted: Thu Jul 13, 2017 2:46 pm
by tele_player
At the very least, your final snippet doesn't allocate any space for
char * s;

So, s is a pointer which doesn't point to any storage.

Re: Preference fetchString not working..... user error?

Posted: Thu Jul 13, 2017 3:33 pm
by martinayotte
You should not using "char *s;" to define your variable, since it is an unallocated pointer, but use "char s[200];" instead ... :ugeek: