title: ESP8266 SPIFFS textfile read returns weird characters
body: <p>I'm trying to store some hex color palettes in the ESP8266's SPIFFS but when I try to retrieve them I get some weird chars. The code and the log output are below:</p>
  
  <p>Code (ino file):</p>
  
  <pre><code>#include "FS.h"                   // https://github.com/esp8266/Arduino/
  #include &lt;JsonParser.h&gt;           // https://github.com/henrikekblad/ArduinoJson
  #include &lt;JsonGenerator.h&gt;        // https://github.com/henrikekblad/ArduinoJson
  
  using namespace ArduinoJson;
  
  void setup(void) {
    Serial.begin(9600);
    SPIFFS.format();
    SPIFFS.begin();
    writeTxtFile("/palettes/warm.json", "[\"ffffff\",\"ed1414\",\"f6f336\",\"ff7e15\"]");
    writeTxtFile("/palettes/pastel.json", "[\"ffffff\",\"ff7b7b\",\"8fff70\",\"7878ff\"]");
    readPalettes();
  }
  
  void loop(void) {
  }
  
  bool readPalettes(void) {
    bool succeeded = true;
    Dir dir = SPIFFS.openDir("/palettes");
    while (dir.next()) {
      File palette = dir.openFile("r");
      if (!readFile(palette, loadPaletteJson)) succeeded = false;
      palette.close();
      yield();
    }
    return succeeded;
  }
  
  bool loadPaletteJson(char* json, String fileName) {
    Serial.print(fileName);Serial.print(F(": "));Serial.println(json);
    Parser::JsonParser&lt;5&gt; parser; // one palette is 1+4*1=5 tokens
    Parser::JsonArray p = parser.parseArray(json);
    if (p.success()) {
      //Store the palettes in a vector using a specific structure
      return true;
    } else {
      Serial.println(F("JSON parsing failed"));
    }
    return false;
  }
  
  bool readFile(File file, std::function&lt;bool (char* json, String fileName)&gt; callback) {
    if (file) {
      String fileName = file.name();
      size_t fileSize = file.size();
      Serial.print(F("Reading "));Serial.print(fileSize);Serial.print(F(" bytes from "));Serial.println(fileName);
      if (fileSize &lt;= 1024) {
        char buf[fileSize];
        file.readBytes(buf, fileSize);
        return callback(buf, getBaseName(fileName));
      } else {
        Serial.println(F("file size is too large"));
      }
    } else {
      Serial.println(F("file open failed"));
    }
    return false;
  }
  
  String getBaseName(String fileName) {
    return fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.'));
  }
  
  bool writeTxtFile(String path, String content) {
    File file = SPIFFS.open(path, "w");
    if (file) {
      Serial.print(F("Writing content: "));Serial.print(content);Serial.print(F(" to: "));Serial.println(path);
      file.print(content);
      file.close();
      return true;
    } else {
      Serial.print(F("file open failed: "));Serial.println(path);
    }
    return false;
  }
  </code></pre>
  
  <p>Arduino's IDE console output:</p>
  
  <pre><code>Writing content: ["ffffff","ed1414","f6f336","ff7e15"] to: /palettes/warm.json
  Writing content: ["ffffff","ff7b7b","8fff70","7878ff"] to: /palettes/pastel.json
  Reading 37 bytes from /palettes/warm.json
  warm: ["ffffff","ed1414","f6f336","ff7e15"]⸮?8⸮?tu @
  JSON parsing failed
  Reading 37 bytes from /palettes/pastel.json
  pastel: ["ffffff","ff7b7b","8fff70","7878ff"]⸮?8⸮?tu @
  JSON parsing failed
  </code></pre>
  
  <p>I don't understand why those chars (<code>⸮?8⸮?tu @</code>) are added to a limited sized buffer. I tried to replace my buffer (<code>char buf[fileSize];</code>) with a unique pointer like this: <code>std::unique_ptr&lt;char[]&gt; buf(new char[fileSize]);</code> (in the function <code>readFile</code>) using <code>buf.get()</code> in the next 2 statements but with the exact same result.</p>
  
  <p>Am I missing something?</p>
  
  <p>EDIT:
  As @leetibbett mentioned below, I'm writing a String while trying to read bytes. here is the correction for <code>readFile</code> function:</p>
  
  <pre><code>bool readFile(File file, std::function&lt;bool (char* json, String fileName)&gt; callback) {
    if (file) {
      String fileName = file.name();
      size_t fileSize = file.size();
      Serial.print(F("Reading "));Serial.print(fileSize);Serial.print(F(" bytes from "));Serial.println(fileName);
      if (fileSize &lt;= 1024) {
        String content = file.readStringUntil('\n');
        return callback((char*)content.c_str(), getBaseName(fileName));
      } else {
        Serial.println(F("file size is too large"));
      }
    } else {
      Serial.println(F("file open failed"));
    }
    return false;
  }
  </code></pre>