Go Down

Topic: Text file on SD to String (Read 5604 times) previous topic - next topic

Nicooo

Hi,

Me again, with my simple problems.  :P
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: [Select]

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: [Select]
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: [Select]
8210111311710111511695771111001016160651171161116210831011169584101109112616049574862

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

Big thanks,
Nico

fat16lib

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

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


PaulS

Code: [Select]
            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: [Select]
char ltr = statusFile.read();
SD_Read += ltr;


fat16lib

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

Code: [Select]
  Serial.print(statusFile.read());

Will print the characters as int.

Nicooo

Thank you PaulS! You're my hero of the day.  :P

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: [Select]
  Serial.print(statusFile.read());

Will print the characters as int.

Also new to me! But good to know, sometime it may become handy!

f.schiano



The output:
Code: [Select]
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: [Select]
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: [Select]
Request_Mode=<Auto>
Set_Temp=<190>


Infact :
R=82
e=101
q=113
...
...
etc!
L'impossibile...richiede solo piu' tempo!

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