# Error while saving data in text file

**URL:** <https://forum.arduino.cc/t/error-while-saving-data-in-text-file/300205>\
**Category:** Programming\
**Created:** [March 25, 2015, 12:57pm UTC](https://forum.arduino.cc/t/error-while-saving-data-in-text-file/300205 "2015-03-25T12:57:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![renatotavares](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/renatotavares/32/1189508_2.png) [@renatotavares](https://forum.arduino.cc/u/renatotavares)\
**Post date:** [March 25, 2015, 12:57pm UTC](https://forum.arduino.cc/t/error-while-saving-data-in-text-file/300205/1 "2015-03-25T12:57:11Z")

</div>

I created a function that saves the timestamp in a text file, but it saves unreadable. Full of invalid characters, example: **Ÿ@Â O ƒº!¥ Ÿ@Â O ƒº!¥ Ÿ@Â O ƒº!¥ Ÿ**

```cpp
#include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include "RTClib.h"
    
    RTC_Millis rtc;
    File txt;
    const char * getCurrentTimestamp()
    {
        char dateBuffer[40];
        DateTime now = rtc.now();
        sprintf(dateBuffer,"%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
        return dateBuffer;
    }
    
    void saveLog()
    {
      txt = SD.open("dados.txt", FILE_WRITE);
      txt.print(getCurrentTimestamp());
      txt.close();
    }
    
    void setup() {
      pinMode(53, OUTPUT);
      SD.begin(4); 
      Serial.begin(9600);
      rtc.begin(DateTime(F( __DATE__ ), F( __TIME__ )));
    }
    
    void loop() {
      Serial.println(getCurrentTimestamp());
      saveLog();
      delay(1000);
    }

```

My code is very simple, do not understand where I went wrong. if I copy the following code snippet for **saveData()** function and save the **dateBuffer** variable, everything works normally.

```cpp
  char dateBuffer[40];
    DateTime now = rtc.now();
    sprintf(dateBuffer,"%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

```

how to save the timestamp correctly in the file through the function **getCurrentTimestamp()**?

---

<div class="post-metadata">

**Author:** ![jurs](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jurs](https://forum.arduino.cc/u/jurs)\
**Post date:** [March 25, 2015, 1:04pm UTC](https://forum.arduino.cc/t/error-while-saving-data-in-text-file/300205/2 "2015-03-25T13:04:36Z")

</div>

Your variable "char dateBuffer[40];" is a LOCAL variable that is ONLY VALID while your function is executed. As soon as the function ends, this variable is no longer valid, the RAM will be released and overwritten.

You can either declare the variable 'static' to become valid all the time:

```cpp
static char dateBuffer[40];

```

Or you can use a global variable to format the timestamp.

---

<div class="post-metadata">

**Author:** ![renatotavares](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/renatotavares/32/1189508_2.png) [@renatotavares](https://forum.arduino.cc/u/renatotavares)\
**Post date:** [March 25, 2015, 1:21pm UTC](https://forum.arduino.cc/t/error-while-saving-data-in-text-file/300205/3 "2015-03-25T13:21:12Z")

</div>

Thank you, my friend

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:12pm UTC](https://forum.arduino.cc/t/error-while-saving-data-in-text-file/300205/4 "2021-05-05T21:12:04Z")

</div>

Closed on May 5, 2021, 9:12pm UTC


