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

chrisvandiemen
Posts: 1
Joined: Wed Jul 12, 2017 10:44 am

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

Postby chrisvandiemen » Wed Jul 12, 2017 10:49 am

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?

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

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

Postby tele_player » Thu Jul 13, 2017 2:46 pm

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.

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

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

Postby martinayotte » Thu Jul 13, 2017 3:33 pm

You should not using "char *s;" to define your variable, since it is an unallocated pointer, but use "char s[200];" instead ... :ugeek:

Who is online

Users browsing this forum: Google [Bot] and 155 guests