XRAD'S read SD txt file convert to String: Solved w/Arduino Education

I have a keypad touch TFT. I can save and delete SD files no problem. I am trying to read the SD txt file which is "000" but could be any txt (digits only) up to 12 places.

I can read the SD no problem and display it on serial monitor.

But, I want to save the txt from the SD into a String variable called String password =" " ; in my sketch.

I am having trouble converting the SD txt into a usable format (because I do not know how to do it) to save to the String variable.

Can anyone point me in right direction??

String password = " ";


  myFile = SD.open("newpword.txt");

  if (myFile) {

    Serial.println("newpword.txt:");// shows the txt "000" on serial

    while (myFile.available()) {

      Serial.write(myFile.read());
  
      password =  myFile.read(); //I know this does not work, but this is what I would like to do
  
    }

    myFile.close();

Thanks!

password.concat( (char)myFile.read() );

By the way, with this code

Serial.write(myFile.read());
  
      password =  myFile.read();

you read 2 char, the one that you print with Serial.write() and the other that you append to password.

use:

char c = myFile.read();

Serial.write(c);

password.concat(c);

Thank you! I tried it and received a "0⸮" instead of "00" ...do I have to terminate this +0 ?

I don't think.

Serial.write print the correct password?

Your code should be

String password = " ";


  myFile = SD.open("newpword.txt");

  if (myFile) {

    Serial.println("newpword.txt:");// shows the txt "000" on serial

    while (myFile.available()) {

      char c = myFile.read();

      Serial.write(c);
  
      password.concat(c); //if Serial.write is ok, this should be ok too
    }

    myFile.close();

using your suggestions in code below this is what I get with first code below. the newpword.txt file in the SD is set to "0000"

serial:
KEYPADLOCK
Initializing SD card...initialization done.
newpword.txt:
0000
Touchscreen started

  myFile = SD.open("newpword.txt", FILE_WRITE);
 
  myFile = SD.open("newpword.txt");
  if (myFile) {
    Serial.println("newpword.txt:"); 
    while (myFile.available()) {
      Serial.write(myFile.read());
      //char c = myFile.read();
      //Serial.println(c);
      //password.concat(c);
    }
  myFile.close();
 }

and this is what I get with code below:

KEYPADLOCK
Initializing SD card...initialization done.
newpword.txt:
00
0
00
00

00

Touchscreen started

 myFile = SD.open("newpword.txt");
  if (myFile) {
    Serial.println("newpword.txt:"); 
    while (myFile.available()) {
      
      Serial.write(myFile.read());
      char c = myFile.read();
      Serial.println(c);
      password.concat(c);
      Serial.println(password); 
    } 
    myFile.close();
 }
while (myFile.available()) {
      
      Serial.write(myFile.read()); // <------------ THIS IS WRONG
      char c = myFile.read();
      Serial.println(c);
      password.concat(c);
      Serial.println(password); 
    }

When you use

      Serial.write(myFile.read()); // <------------ THIS IS WRONG

you read a char. This char is gone, bye bye.

      char c = myFile.read();

Now you read another char, so you skip the odd chars of your password and store only the even chars.

Remove this from your code

      Serial.write(myFile.read()); // <------------ THIS IS WRONG

Ok so by saying THIS IS WRONG x3 :grinning: (i have a very small brain and kept adding that line back in since post #3) i finally got the picture THX!!!!

myFile = SD.open("newpword.txt");
  if (myFile) {
    Serial.println("newpword.txt:"); 
    while (myFile.available()) { 
      char c = myFile.read(); 
      SDpassword.concat(c); 
    } 
    myFile.close();
    Serial.println(password);//out here so your don't see all the concat
    password = SDpassword;
  }

THX!! :grin: +karma

I am not sure what changes to the data and how to fix it. When I use:

SD card has .txt file "0000" already on it.

String password = "0000" ; if in the original setup, everything works fine

when I use:

String SDpassword =""; and this populated with the char from above SD file(post #6), no problem, shows up fine back on serial monitor 100%correctly as 0000

BUT, when I do this:

password = SDpassword;// to replace initial String password ="";

the password AND SDpassword both show up 100% correct on the serial monitor, but the sketch will not accept the SDpassword substitute as the correct new password

Serial monitor output following replacement of password with SDpassword looks like this:

KEYPADLOCK
Initializing SD card...initialization done.

newpword.txt: 0000

SDpassword...0000

password...0000

I'm an not sure what changed to the data here and why the SDpassword, even when looking just like password on serial monitor, has a different underlying structure.....

I figured it out...mostly. Now my approach is probably not the best code practice, but I have no idea because I am a beginner. So what I did was use vic0617's method to extract a char from the .txt file, and then this is concatenated so the data from the file is all there in one line.

so for example, on boot, the SD.txt file 7777 is now SDpassword 7777.

The key was to then take this SDpassword 7777 and use:

int SDpasswordValue = SDpassword.toInt();

and then save this as the new password.

which works perfectly. I can boot with a new SDpassword, I can change the current SDpassword, and reboot with new SDpassword replacing old password.

Nice!

here is working code block if anyone is interested:

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//XRAD's Frankencode NOTE:
//if there is a newpword.txt file available, it will be used to replace the current password.
//If there is no newpword.txt file available, the String password = "0000"; is used
///////////////////////////////////////////////////////////////////////////////////////////////////////////

//open the SD.txt file
  myFile = SD.open("newpword.txt");
  if (myFile) {
    Serial.println("newpword.txt:"); 
    while (myFile.available()) {
      char c = myFile.read();
      SDpassword.concat(c);
    }

    int SDpasswordValue = SDpassword.toInt();//key conversion for this to work

    // make the SDpassword the new password
    password = SDpasswordValue;

    myFile.close();

    Serial.print("SDpassword...");//  see what the concat produces
    Serial.println(SDpassword); //with the replaced password
  }
  else { 
    Serial.println("error opening newpword.txt");
  }

  //password = tempPassword;
  Serial.print("password...");
  Serial.println(password);
  Serial.print("tempPassword...");
  Serial.println(tempPassword);

if you forget the code, you can just pop out the SD and look at the .txt file, or create a new .txt code (1-9 digits long and not starting with a '0'!),

I also tried:
String myString = String( SDpassword); //compiles but does not work
password = myString;//compiles but does not work

Just wanted to update this in case anyone following. Sitting on my desk for a week was a 3 page print of arduino education exmple>Strings

https://www.arduino.cc/en/Tutorial.StringToIntExample

I just don't complete the integer conversion in the example above.

so instead of reading serial monitor, i just read the txt file. works just fine now and I can use any code even starting with '0' and in Digits. makes sense.

My current password on boot stores and reads the txt file fine and I can use all 12 digit spaces including 11 '0's with a '1' at the end!! :slight_smile:

Serial data:

Success!
COLOR CHANGE
KEYPADLOCK
Initializing SD card...initialization done.
newpword.txt:
SDpassword...000000000001
password...000000000001
tempPassword...
Touchscreen started

/////////////KEYPADLOCK///////////////////////////////////
  Serial.println("KEYPADLOCK");
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {// for feather M0
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  delay (500);

  //  open the SD file for reading:
  myFile = SD.open("newpword.txt");
  if (myFile) {
    Serial.println("newpword.txt:");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      int inChar = myFile.read();
      if (isDigit(inChar)) {
        inString += (char)inChar;
      }
    }
    password = inString;

    myFile.close();
    Serial.print("SDpassword...");
    Serial.println(SDpassword);
  }

  else {
    // if the file didn't open, print an error:
    Serial.println("error opening newpword.txt");
  }

  //password = tempPassword;
  Serial.print("password...");
  Serial.println(password);
  Serial.print("tempPassword...");
  Serial.println(tempPassword);