1 Reply Latest reply on Apr 24, 2015 7:22 PM by crmakers

    How to read the content of a file into an String in ARDUINO Sketch

    crmakers

      Hi,

       

      How can I read the content of a file into a variable of type String in an ARDUINO sketch?

       

      I've been trying with the following code, but I am having trouble setting the content as a well defined string I can later compare in an if or switch statement.

       

      Thanks,

       

      --jn

       

      String readFileValue(){
        FILE *filePointer;
        int currentChar;
        char initCharArray[100];
        int i = 0;
         
        filePointer = fopen("/storedValue.txt","r");
        while(1) {
          currentChar = fgetc(filePointer);
          if( feof(filePointer) )
          { 
            break ;
          } else {
            initCharArray[i] = currentChar;
            i++;
          }      
        
        }
        fclose(filePointer);
         
        Serial.println(initCharArray);
        return(0); //Forget about this line for now
      }
      

       

      The file just contains the "HIGH" string. That function prints in line 21 the following:

      HIGH
      Æø¿z*ÂN 
      

       

      Then I cant find how to (1) Properly craft the initCharArray and (2) Properly cast it to a String type.