converting a string to int - reading file from SD Card

Hi All,

I'm using some code i found to read a simple text config file from an SD Card

In my example I have one integer value and two strings

The strings read and display fine but the integer doesn't seem to work and always goes to the default value in the code

Can someone look at this code snippet and suggest what I am doing wrong?

The string on the SD card that is being read is TTP = 10 but this code ALWAYS returns 60

if(description == "TTP") {
          value = "";
          while(character != '\n') {
            if(isdigit(character)) {
              value.concat(character);
            } else if(character != '\n') {
              // Use of invalid values
              valid = false;
            }
            character = myFile.read();            
          };
          if (valid) { 
            // Convert string to array of chars
            char charBuf[value.length()+1];
            value.toCharArray(charBuf,value.length()+1);
            // Convert chars to integer
            settings.TTP = atoi(charBuf);
          } else {
            // revert to default value for invalid entry in settings
            settings.TTP = 60;
          }

Thanks in advance - still learning the ins and outs of Arduino and get stuck on seemingly simple things :frowning:

Bah found it........DOH

had to change:

if (valid) {

to

if (valid = true) {

Sorry all!

had to change:

if (valid) {

to

if (valid = true) {

The first condition is OK but second not it should be "if(valid == true)", but in fact both will be the same.

...and

if(description == "TTP") {

In C language you can't compare string by this way. You have to compare per a character or use string function (e.g. strcmp) which do the same.

Hmmm......confusing!

First didnt work

Second did

Code works fine now!

Code works fine now!

Nonsense. Unless you've changed the code and didn't post the changed code.

Snippets don't cut it here. We need to see ALL of your code, so that we can see if if(description == "TTP") is legitimate. If description is a string, it is not. If description is a String, it is, but you should be ashamed of yourself for using Strings.

Only change made was as above......

    if(description == "TTP") {
          value = "";
          while(character != '\n') {
            if(isdigit(character)) {
              value.concat(character);
            } else if(character != '\n') {
              valid = false;                                     // Use of invalid values
            }
            character = myFile.read();            
          };
          if (valid = true) { 
            char charBuf[value.length()+1];                      // Convert string to array of chars
            value.toCharArray(charBuf,value.length()+1);
            settings.TTP = atoi(charBuf);                        // Convert chars to integer
          } else {
            settings.TTP = 0;                                   // revert to default value for invalid entry in settings
          }

If using strings is "wrong" how do I read a file from SDCARD that contains a configuration file where the descriptions for each configuration is a string, and the configuration item may also be a string??

If using strings is "wrong"

Pay attention this time. Using strings is not wrong. Using Strings is. Big f***ing difference.

Only change made was as above......

Right. You changed the code ABOVE what we can see. Take your snippets elsewhere. Maybe http://snippets-r-us.com can help you.

FFS.........SORRY TO BOTHER YOU PAUL!

A leaner confused by simple syntax errors such as use of capitalisation (coming from other languages where it is NOT so finiky) is given this kind of "welcome" -no wonder people just give up.

Kind of defeats the whole purpose of community doesnt it????

Stocky:
A leaner confused by simple syntax errors such as use of capitalisation (coming from other languages where it is NOT so finiky) is given this kind of "welcome" -no wonder people just give up.

Kind of defeats the whole purpose of community doesnt it????

just keep trying, there are people here to help.

I am in China right now, so i invented a Confucianism for you.

Confucius Bulldog says: " A gold nugget covered in dog poop is still a gold nugget"

so clamp your nose and get the nugget!!!!

@Stocky: Paul has almost 50 thousand posts garnering over six hundred karma points...he's kinda earned the right to get a little miffed when someone doesn't read what he has to say carefully. You chose to post on this Forum where the language d'jour is finicky about capitalization. You should be happy that Paul tried to help and, as Bulldog pointed out, accept the fact that you're the one asking for help and Paul is one of the sharper knives in this drawer and usually has something worthwhile to say.

Thanks guys - and apologies to Paul - no sleep and the flu makes me grumpy. But also a gentle nudge in the right direction goes further than a crack across the back of the head......

The capitalisation things keeps catching me out and drives me nuts! Seriously I had to re-read Pauls post a few times to see the difference in what he wrote :frowning: The human brain works against you sometimes.

I'll try harder next time - and spend two hours making the rest of my code postable so I dont just post a snippet :stuck_out_tongue:

On the other hand I think the behavior of PaulS toward new forum members is becoming an increasing forum embarrassment to me and maybe other members of the forum. Sad. :roll_eyes:

I have another Bulldog Confucianism:

"One may never forget the agony of a bee sting, yet one cannot survive in a world without bees."

this is fun... :blush: