0

I have a config.json file stored in the Flash SPIFFS memory of my ESP8266-based web server.

My intent is to read this file into a char array (char string[]), avoiding the use of Arduino's String, and send it to the client after an Ajax call.

The config.json is something like this : {"start1h":11,"start1m":20,"stop1h":15,"stop1m":40}

I've tried with the following code, but the problem is that the client receives a string with an '@' character at the end and fails to parse the string via javascript

#define CONFIGFILE "/config.json"
File configFile = SPIFFS.open(CONFIGFILE, "r");

size_t filesize = configFile.size(); //the size of the file in bytes     
char string[filesize + 1];   // + 1 for '\0' char at the end      

configFile.read((uint8_t *)string, sizeof(string));  
configFile.close(); 
string[filesize + 1] = '\0';
Serial.print(string);    

server.send(200, "text/plane", string);

Serial output: {"start1h":11,"start1m":20,"stop1h":15,"stop1m":40}@

Client receives ajax response :{"start1h":11,"start1m":20,"stop1h":15,"stop1m":40}@

'@' at the end of string!

What's wrong with my code? Thanks in advance

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
matbosco
  • 3
  • 1
  • 4

1 Answers1

2

I realize this question was months ago, but I came across your question when searching for help getting a similar system working, and I think I spotted the error in your code.

You're putting the '\0' null character in beyond the end of the array.

If the array has filesize + 1 elements, the last element where you need to insert the null character is string[filesize], rather than string[filesize + 1].

MarredCheese
  • 17,541
  • 8
  • 92
  • 91