0

http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html

I am trying to figure out that if we use eeprom_write_block function or eeprom_update_block function do we need to manually null terminate it?

I came across this tutorial on avr-freaks where in one comment it is said that you have to read the strings and null terminate them but there is no word about writing. Couldnt get anything the avr-libc manual as well.

UPDATE

I have reserved some parts of eeprom using EEMEM and they are strings. I am not sure how long the string would be except that it would be smaller than a max value. Now When I would read the string back from the eeprom I would just be reading the maximum number of bytes as again I would have no idea of the size of the string. If the eeprom has a null value at the end of the string when writing to it, the read string will have that null at the correct place.

Situation

I am saving mobile numbers of users as:

uint8_t EEMEM users[numberOfUsers][maxChar4Number];

Now when I get a number parsed and then save it to eeprom

#define eepu(message, EEADDR, BLKSIZE) eeprom_update_block((const void *)message, (void *)EEADDR, BLKSIZE);

eepu(user, users[0], strlen(user)); //strlen(user) will be less than the maxChar4Number

This will write the number to eeprom. When I read the i would be reading maxChar4Number bytes and a number could have been shorter than that length.

2 Answers 2

1

If you write a block which happens to be null-terminated, then reading it back should be fine. I do that when I want to save a filename in EEPROM. Just declare a suitable length (allowing room for the null terminator) and write the entire block to EEPROM.

1

Since one normally does not know how long a character string is, we need a way to convey this information. One technique used to avoid confusion is to add a NULL character (which is a zero) to the end of the string. Another is to explicitly state how long the string is.

In the case of the function eeprom_write_block() talked about here, we see that we are formally passing not only a pointer to the string but also the size of the string.

3
  • I am using ` eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));`. it has the size argument . do I need to Null terminate??
    – dmSherazi
    Jun 30, 2016 at 22:49
  • I have reserved some parts of eeprom using EEMEM and they are strings. I am not sure how long the string would be except that it would be smaller than a max value. Now When I would read the string back from the eeprom I would just be reading the maximum number of bytes as again I would have no idea of the size of the string.
    – dmSherazi
    Jun 30, 2016 at 22:53
  • I would think you do not need a NULL at the end of your character string for the eeprom_read_block() method as you are telling it how many characters you are reading. I don't think you need it for the sizeof() method call either as sizeof() uses the type of the argument. That said, I believe you will always read the size of your array number of characters from EEPROM. Is that what you want?
    – st2000
    Jul 1, 2016 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.