Pages: [1]   Go Down
Author Topic: Storage the text inside of the SD card to string  (Read 1041 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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");
  }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49446
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49446
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char c = myFile.read();
Serial.write(c);

myResourceWaster += c;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();
 }
Logged

Pages: [1]   Go Up
Jump to: