converting character strings to numbers

I would like to be able to read configuration parameters for a datalogger application from a text file stored on an SD card -- something like this:

3,3.3,9600,OUTFILE.CSV

I know how to read this string from a file and even how to break it into separate strings using the commas as delimiters. In other languages it would be easy to process this string as int, float, int, and string values. But the Arduino language seems to have no capability to convert the strings containing numbers into their numerical values. Is there a way to do this? Perhaps with atoi() and atof(), but I can't get that to work. Can you help?

In other languages it would be easy to process this string as int, float, int, and string values.

So, use the same processes as you would in C

Perhaps with atoi() and atof(), but I can't get that to work.

Can you please post a program showing what you tried and details of what did not work.

Here is the code:

#include <SD.h>
#include <String.h>

File myFile;
char fileName[]="CONFIG.TXT";
String configString="3,9600,3.3,logger_file";
int fileByte,commaPosition;
//char * x[20];
int a1,a2;
float a3;
//char x4[20];

void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Serial.println(configString);
Serial.print("length: ");
Serial.println(configString.length());
do {
commaPosition=configString.indexOf(',');
if (commaPosition!= -1) {
Serial.println(configString.substring(0,commaPosition));
configString=configString.substring(commaPosition+1,configString.length());
}
else {
if (configString.length()>0) {
Serial.println(configString);
}
}
} while (commaPosition>=0);
}
void loop()
{
}

Here is the output:
Initializing SD card...initialization done.
3,9600,3.3,logger_file
length: 22
3
9600
3.3
logger file

What Now? How can I interpret 3 and 9600 as integers, 3.3 as a floating point number, and logger file as a string? Nothing that I've tried has worked at all.

Now that I have seen your code I see that you are using Strings, not strings. Not the same thing at all.

Have you been trying to use atoi() and atof() with Strings ?

Yes, I have tried both String and string, and atoi() and atof(). I have also tried using Serial.parseInt() to get the integer value before the first comma as I read the file -- I will worry about the floats later. I'm sure some combination of these things will work, but I certainly can't figure it out.

Ditch the use of Strings unless you want support from only 1 person on these forums (zoomkat).

Post the code you tried that involve strings, and use CODE tags.

Arrch:
Ditch the use of Strings unless you want support from only 1 person on these forums (zoomkat).

Post the code you tried that involve strings, and use CODE tags.

You got code? Didn't think so! Good to see you direct the support of the other forum members. :wink:

Below is some old servo code that converts a String (readString)into an integer.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}

I just can't be doing with all this modern-fangled String stuff

const byte MAX_TOKENS = 20;
const char* delimiters = ", "; // whatever characters delimit your input string
char* tokens [MAX_TOKENS + 1];

char* inputSentence = "3,9600,3.3,logger_file";

void setup ()
{
  Serial.begin (115200);

  int tokenIndex = 0;
  Serial.print (F("Received <"));
  Serial.print (inputSentence); // tell 'em what you've got
  Serial.println (F(">"));
  Serial.print (F("Length "));
  Serial.println (strlen (inputSentence));
 
  tokens [tokenIndex] = strtok (inputSentence, delimiters);
  while ((tokenIndex < MAX_TOKENS - 1) && tokens [tokenIndex])
  {
    tokens [++tokenIndex] = strtok (NULL, delimiters);
  }
  for (int i = 0; i < tokenIndex; i++)
  {
    Serial.print (tokens [i]);
    if (isdigit (tokens [i][0]))
    {
      Serial.print (F("  numeric"));
      if (strstr (tokens [i], "."))
      {
        Serial.print (F(" floating-point x = "));
        float x = atof (tokens [i]);
        Serial.println (x);  
      }
      else
      {
        Serial.print (F(" integer x = "));
        int x = atoi (tokens [i]);
        Serial.println (x);
      }
    }
    else
    {
      Serial.println (F(" string"));
    }
  }
}

void loop ()
{
}