Go Down

Topic: Storage the text inside of the SD card to string (Read 1 time) previous topic - next topic

WillerKrautz

That is may problem, I can not read the content from the file to inside of String variable.
I would like to know if somebody can help me, because I need to send the information trough the net for the central computer
and I cant because I just receive 1111111111 instead the ASCII characters.
Thanks.

String var1="";
     .
     .
     .
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
       var1 += Serial.write(myFile.read());   ////////  I try that but I just receive 111111111111
    }
    // close the file:
    myFile.close();
  } else {
     // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

PaulS

Code: [Select]
   var1 += Serial.write(myFile.read());   ////////  I try that but I just receive 111111111111
myFile.read() gets a character from the file. Serial.write() does something with that character and returns the number of bytes it dealt with. I can't imagine why you want to append that value to the end of a String. I also can't imagine why you are surprised that you get nothing but a string of ones.

WillerKrautz

Thank you PaulS for replay.
My real problem is, I need to storage on STRING variable, the text found inside of the file, that text is very small. The reason is because  I need to send that information to the connected PC on my network with ethernet shield.
You are right when you say I am surprised about ones coming into the variable, the reason is because I am a beginner on Arduino system, I want to learn more about that and I need help. Please if you know how to do that I appreciate.
Thank you in advance.

WillerKrautz

if it can helps I found that code but my display showed me blocks instead characters
     lcd.print((char)Serial.read());
if I do this :      lcd.print((String)Serial.read()); the result is -1 for each character on SD file.
I still with no answers.

PaulS

Quote
My real problem is, I need to storage on STRING variable

No yOu doN't. There are Strings and there are strings on the Arduino. There are no STRING variables.

Read the reference page on File::read(). Read the reference page on Serial.write(). The return value from Serial.write() is NOT the character written. It is the number of characters written.

Code: [Select]
char c = myFile.read();
Serial.write(c);

myResourceWaster += c;

WillerKrautz

Well, I found this code and works for me. Thanks for the help.

String Room="";
   .
   .
   .
  if (myFile)
  {
    char character;
    while (myFile.available())
     {
        character = myFile.read();
        if(character == '/')       
          {  // Comment - ignore this line
           while(character != '\n')
           {
              character = myFile.read();
           }
          }
        else if(isalnum(character))        // Add a character to the description
           {   
        Room.concat(character);
           }
     } 
        myFile.close();
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy