Need help with reading EEPROM

I wrote this program myself, it is pretty easy to understand its use. :

#include <EEPROM.h>

bool writeSSID(String _SSID,String password,byte security)
{
  byte ssid_length = _SSID.length();
  byte password_length = password.length();
  if(ssid_length < 1 || password_length < 8 && password_length != 0)
  {
    return false;
  }
  else if (ssid_length > 32 || password_length > 64)
  {
    return false;
  }
  EEPROM.write(0,ssid_length);
  EEPROM.write(1,password_length);
  char ssid[ssid_length];
  char password_[password_length];
  _SSID.toCharArray(ssid,ssid_length);
  password.toCharArray(password_,password_length);
  for(int i = 2,n = 0;i <= ssid_length;i++, n++)
  {
    EEPROM.write(i,ssid[n]);
  }
  for(int i = ssid_length + 2, n = 0;i <= password_length;i++, n++)
  {
    EEPROM.write(i,password_[n]);
  }
  EEPROM.write(2+password_length+ssid_length,security);
  return true;
}
// index 0 = ssid,index 1 = password,index 2 = security
String readSSID (byte index)
{
  String output[3];
  byte ssid_length = EEPROM.read(0);
  byte password_length = EEPROM.read(1);
  char ssid[ssid_length];
  char password[password_length];
  byte security;
  for(int i = 2;i <= ssid_length;i++)
  {
    ssid[i-2] = EEPROM.read(i);
  }
  for(int i = ssid_length + 2, n = 0;i <= password_length;i++, n++)
  {
    password[n] = EEPROM.read(i);
  }
  security = EEPROM.read(password_length+ssid_length+2);
  output[0] = ssid;
  output[1] = password;
  output[2] = security;
  return output[index];
  //for
}
void setup() {
  Serial.begin(9600);
  if(writeSSID("TEEST","12345780",0))
  {
    Serial.println("Successfully Written to EEPROM !");
  }
  else 
  {
    Serial.println("Failed to write !");
  }
}

void loop() 
{
 Serial.println("ssid : " + readSSID(0));
 Serial.println("pass : " + readSSID(1));
 Serial.println("Security Type : " + readSSID(2));
 delay(5000);
}

Output :

Successfully Written to EEPROM !
ssid : TEESxB
pass : 12�!¹!ÌTEESxR
Security Type : 0

Security Type is working, since it was the easiest.
Seems like first 6 bytes in the EEPROM read fine, the jumbled data comes after that (with every password/ssid I’ve tried)
I would like if you guys would help me with the error in my logic. It’s my first time handling streams like this (never did this in higher level langs)

  char ssid[ssid_length];
  char password_[password_length];
  _SSID.toCharArray(ssid,ssid_length);
  password.toCharArray(password_,password_length);

If the password is “Wrong”, that’s 5 characters. As a string, that’s 6 elements in the array - you MUST account for the NULL terminator. Your arrays are too small, so the toCharArray() method is writing past the end of the arrays.

  for(int i = 2,n = 0;i <= ssid_length;i++, n++)

The while clause (the 2nd one) should involve the array index, NOT the EEPROM address.

I would like if you guys would help me with the error in my logic.

The error in your logic is assuming that it is OK to use Strings. It isn’t. Get over it.

I found the perfect solution.

lesson learnt