-1

I know that there is a lot of subject regarding the variable conversion but I spend more than 2 hours on different forum and I don't find how to solve my problem ...

I've got a code which is going to read on SPIFFS file on my ESP32 and write it into the Serial monitor. (Here we are on the sample code given on many website)

But now how can I send the value of file.read() into a const char* I really need this format because of the function which will receive the value ...

My code :

const char* VALUE     = "";

File file = SPIFFS.open("/test.txt");
      if(!file){
          Serial.println("Failed to open file for reading");
          return;
      }

      while(file.available()){
        Serial.write(file.read());
        VALUE = file.read();
      }
      file.close();

This result as : invalid conversion from int to const char*

flyer74
  • 195
  • 1
  • 12
  • 1
    VALUE is a const char* the const part stands for "constant" in case that isn't obvious. The constant thing means that you can't assign a value to it once it already has one. In your code you've already assigned it the value "", an empty string. You can't come back and give it a different value now at runtime. – Delta_G May 24 '20 at 15:44
  • 1
    What function are you trying to send VALUE into? Perhaps we should look at that instead. I'm betting what you need to do is read values from serial into a char array and then use the char array. But without seeing the rest of the code it's just a guess. – Delta_G May 24 '20 at 15:45

1 Answers1

2

Try the following:

char VALUE [512] = {'\0'};  // 511 chars and the end terminator if needed make larger/smaller

File file = SPIFFS.open("/test.txt");
  if(!file){
      Serial.println("Failed to open file for reading");
      return;
  }
  uint16_t i = 0;
  while(file.available()){
     VALUE [i] = file.read();
     // Serial.print (VALUE [i]); //use for debug
     i++;
  }
  VALUE [i] ='\0';
  // Serial.print (VALUE); //use for debug
  file.close();

This copies the content of the file into a char array, which can then be used for further processing. If the char array is defined globally it is compiled to flash and prevents memory fragmentation

Codebreaker007
  • 2,911
  • 1
  • 10
  • 22
  • Oh yeah ! This work very well ! I've never use array in Arduino before now I understand how to use it with char ! Thanks ! – flyer74 May 24 '20 at 16:27