Write and read char, string, int array for EEPROM

hello happy good day!

please help me to create code write and read eeprom but my variable is char, string, int array

this is my variable

#include <EEPROMex.h>

int port = 988899;
char server[] = "51.12.999.222";
String Room = "1";
String House[8] = {"1","13","15","16","17","3","19","20"};
int net[8] = {1,2,7,8};
int resetloop = 2;
void setup() {

// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}

1 Like

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

My advice would be to use C style strings (zero terminated arrays of chars) rather than Strings (objects of the String library)

Then you can use the standard EEPROM library EEPROM.put() to save any of your data types with a single call and EEPROM.get() to retrieve any of them with a single call

If the data is in a struct then you load and save the whole struct with a single call to get() and put()

2 Likes

it looks like you didn't even try to look at the examples. Please read the EEPROMEx.ino example

1 Like

Why use EEPROMex when you can use the standard EEPROM library ?

image

3 Likes

hello thank you so much, please give me one example :pray:
String Room = "1";

You don't need a string or a String for that

char Room = '1';

Seriously, read and reread the examples, then come back and ask questions related to those for concepts you don't understand.
C

Let us respect what the OP has desired.

1 Like

The above declaration is certainly wrong. An int type variable can hold data in the range of: -32768 to 32767.

Let's not be silly and declare a string that takes 2 characters to hold one, shall we ?

There are many ways of solving a problem. It is OP's liberty to choose the String data type.

1 Like

I assume then that it also his liberty to put a value of 988899 in a signed int

Can liberty be enjoyed without discipline?

You tell me

You applied discipline to the fact that an int was given a value of 988899

I applied discipline to the fact that a String was used to hold a single character

Either both are correct or both are incorrect
Which is it ?

I applied discipline to the notion that OP is qualified to use String as a data type. Here, he is enjoying liberty. This does not beak the rules of the Programming Language.

An attempt by the OP to store 988899 in an int type variable is something like breaking the discipline (the rules of the Programming Language).

Does the above Room variable (the array) contain single character or two characters?

That, of course, is not strictly true in all cases
For instance

void setup()
{
  Serial.begin(115200);
  int port = 988899;
  Serial.print("value of port variable : ");
  Serial.println(port);
}

void loop()
{
}

prints the following when I try it on one of my test systems

value of port variable : 988899

Of course, the Devil is in the detail and the OP will probably not get the same result if he/she tries it

Neither

Try

void setup()
{
  Serial.begin(115200);
  String Room = "1";
  Serial.print("bytes used by Room String : ");
  Serial.println(sizeof(Room));
}

void loop()
{
}

Does the result surprise you ?

The above is @UKHeliBob's own declaration. Can he sanguinely say that he has not broken the rule of the Programming Language?

We go by the definition of a string, which says that a declared string like "1" contains the character 1 and the null-character.

I can say with hand on heart that I have not broken the rules of the programming language and that the sketch that I posted was compiled using the Arduino IDE and that the output was copied from the IDE Serial monitor. Remember what I said

Can you guess what that detail might be ?

That is very true of a string but we are not talking about strings are we ? We are talking about using a String. How many bytes are used to store a single character String ?

Compiler is a Machine who is here to assist a Human Being to take sound decision based on the results delivered by the Compiler. It is the Human Being who is eventually accountable to the Society and not the Machine for the actions. So, there is no scope to tell the Boss that the Computer (the Machine/Compiler) has delivered me an error free result which I have applied in my project; but, it has proved fatal. I am sure that the Boss will say why have I not exercised my own judgement to see if the Computer has indeed provided the "just" result or not before applying it in an application?

By definition, I have learnt that a string (characters glued together one after another) contains a built-in null-character. Is it correct in saying that the said definition will appear invalid if the data type changes from char to String?

When I compile the code in reply #16 using the Arduino IDE and one of my test systems I get no warnings and certainly no errors, even when verbose errors for compiling and uploading are turned on, so the compiler gives the code a clean bill of health from the point of view of actually compiling the code.

However, the compiler cannot know what I am really doing so it is up to me to ensure that the result is reasonable and that I have stayed within the bounds of arrays and that appropriate data types have been used to hold the values used by and resulting from the sketch, which in this case I have

The definition and your understanding of the structure and contents of a C style string will remain valid if the variable is declared as a string. They will remain the same if you declare a variable as a String but, the structure and contents of a String are not the same as a string.

A String has an array of chars at its heart but the String object takes more bytes to hold the data (see reply #16) which is one reason to avoid using Strings and certainly a reason not to use single character Strings

1 Like