converting String to const char *

Status
Not open for further replies.

AdmiralCrunch

Well-known member
Hi

I read a line from startCfg.txt on my SD-card, .. I split it by ":" , and then put the slices into String-Variables.

Code:
  // READ INIT-CONFIG
  startCfg = SD.open("startCfg.txt");
  if (startCfg) {
    // read each line
    while (startCfg.available()) {
      helpVarString = startCfg.readStringUntil('\n');
      // split line by :
      cfgName = getValue(helpVarString, ':', 0);
      cfgValue = getValue(helpVarString, ':', 1);

      if(cfgName == "MappingPreset") {
        // load cfg file
        loadCfgFile(cfgValue);
      }

    }
    startCfg.close();
  } else {
    Serial.println("error opening startCfg.txt");
  }

Now I push the cfgValue as a argument to loadCfgFile(), where I want this file to be loaded:

Code:
void loadCfgFile(String cfgFile) {
Serial.println(cfgFile); // contains Sring "Default"
 
  loadedCfg = SD.open(cfgFile + ".txt"); // should read Default.txt
  if (loadedCfg) {
    Serial.println("Loading OK");
    }
    startCfg.close();
  } else {
    Serial.print("error opening "); Serial.print(cfgFile); Serial.println(".txt");
  }
  
}

this does not work. As I can see in the https://github.com/PaulStoffregen/SD/blob/master/SD.cpp the open()-method expects a const char *
Code:
File SDClass::open(const char *filepath, uint8_t mode)
.. how would I convert it? or is there a other solution?
 
I personally try to avoid using string objects, but I think you can do something like:
Code:
void loadCfgFile(String cfgFile) {
Serial.println(cfgFile); // contains Sring "Default"
  String filename = cfgFile + ".txt";
  loadedCfg = SD.open(filename.c_str()); // should read Default.txt
  if (loadedCfg) {
...
Hopefully someone who uses them more can give you alternatives
 
I personally try to avoid using string objects, but I think you can do something like:
Code:
void loadCfgFile(String cfgFile) {
Serial.println(cfgFile); // contains Sring "Default"
  String filename = cfgFile + ".txt";
  loadedCfg = SD.open(filename.c_str()); // should read Default.txt
  if (loadedCfg) {
...
Hopefully someone who uses them more can give you alternatives

Hey Kurt,

that worked perfectly! .. thank you so much! :)
 
Status
Not open for further replies.
Back
Top