Pages: [1]   Go Down
Author Topic: Text file on SD to String  (Read 3894 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Me again, with my simple problems.  smiley-razz
Here's my story: I want to store my config file as a .txt file on my SD card.

So i made i file status.ini on my SD, with 2 lines of text in it.
Now i just want to read it for the SD line by line in a string, so i can get my settings out of it.

I made a little demo code. (Adapted for the Datalogger example from the SD lib.)
Code:
void setup(void)
{
  //Start Serial Port
    Serial.begin(9600);
   
  //Read Config File from SD
      //Disable Ethernet shield
        pinMode(Eth_SPI_Select, OUTPUT);
        digitalWrite (Eth_SPI_Select, HIGH);
       
      //Enable SD.
        pinMode(SDC_SPI_Select, OUTPUT);
        digitalWrite (SDC_SPI_Select, LOW);
       
      //Init SD Card 
        Serial.print("Initializing SD card...");
       
        if (!SD.begin(SDC_SPI_Select)) {
            Serial.println("Card failed, or not present");
            // don't do anything more:
            return;
            }
        Serial.println("card initialized.");
       
        File statusFile = SD.open("status.ini");

        // if the file is available, write to it:
        if (statusFile) {
          while (statusFile.available()) {
            Serial.write(statusFile.read());
            //SD_Read=SD_Read+(statusFile.read(),OCT);
          }
         statusFile.close();
        } 
        // if the file isn't open, pop up an error:
        else {
        Serial.println("error opening status.ini");
        }
        Serial.print(SD_Read);
}

This codes, reads the data from the SD and puts it in my serial monitor.
The output:
Code:
Request_Mode=<Auto>
Set_Temp=<190>
So far i'm fine, it succesfully reads from the SD.
But now i want to put this output into a string, so i uncomment the "//SD_Read=SD_Read+statusFile.read();" line and comment the "Serial.write(statusFile.read());" line. Now the output of SD_Read is:
Code:
8210111311710111511695771111001016160651171161116210831011169584101109112616049574862

What kind of format is this? Any tips how to fix this?

Big thanks,
Nico
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1473
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

statusFile.read() returns an int.  Try casting it to a char.

Code:
  SD_Read=SD_Read + (char)statusFile.read();

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
            Serial.write(statusFile.read());
This should be Serial.print(), not Serial.write().

Quote
But now i want to put this output into a string, so i uncomment the "//SD_Read=SD_Read+statusFile.read();" line and comment the "Serial.write(statusFile.read());" line. Now the output of SD_Read is:
That snippet does not tell us what type SD_Read is. If we assume that it is a String object, the value returned by statusFile.read() will be appended to the String object. But, the value will be appended as a string, not a character. The string will be created by converting the numeric value read ('a' = 97) to characters, '9' and '7'. Most likely, this is not what you want.

It happens because the read() method returns an int, and the String class appends an int by converting it to a string and appending the string.

What you need is:
Code:
char ltr = statusFile.read();
SD_Read += ltr;
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1473
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to read characters from a file and copy characters to Serial you want write not print as PaulS suggested.

Code:
  Serial.print(statusFile.read());

Will print the characters as int.
Logged

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

Thank you PaulS! You're my hero of the day.  smiley-razz

Simple answer, but i was looking over it!
Thank you! Have a nice day!

If you want to read characters from a file and copy characters to Serial you want write not print as PaulS suggested.

Code:
  Serial.print(statusFile.read());

Will print the characters as int.
Also new to me! But good to know, sometime it may become handy!
Logged

Germany
Offline Offline
Edison Member
*
Karma: 1
Posts: 1353
Here we go!!!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


The output:
Code:
Request_Mode=<Auto>
Set_Temp=<190>
So far i'm fine, it succesfully reads from the SD.
But now i want to put this output into a string, so i uncomment the "//SD_Read=SD_Read+statusFile.read();" line and comment the "Serial.write(statusFile.read());" line. Now the output of SD_Read is:
Code:
8210111311710111511695771111001016160651171161116210831011169584101109112616049574862

What kind of format is this? Any tips how to fix this?

Big thanks,
Nico

Those numbers are the decimal values corresponding to :
Code:
Request_Mode=<Auto>
Set_Temp=<190>

Infact :
R=82
e=101
q=113
...
...
etc!
Logged

L'impossibile...richiede solo piu' tempo!

Pages: [1]   Go Up
Jump to: