-1

I have a .txt file loaded into the SD card which contains:

SampleTime: 100
SampleInterval : 1000
Phone: 91987654331

I am reading this using the readStringUntil(':'); in Arduino IDE but it reads the whole content together, but I want to split the string and integer and store it in different variables. I want to know how I can split it easily and store it in different variables. Below is my code:

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

  if (SDfound == 0) {
    if (!SD.begin(4)) {
      Serial.print("The SD card cannot be found");
      while(1);
    }
  }
  SDfound = 1;
  printFile = SD.open("consta.txt");

  if (!printFile) {
    Serial.print("The text file cannot be opened");
    while(1);
  }

  while (printFile.available()) {
    buffer = printFile.readStringUntil(':');
    Serial.println(buffer); //Printing for debugging purpose         
    //do some action here
  }
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • what do you mean by: "it reads the whole content together" ? doesn't your sketch print "SampleTime" at separate line? and "100 SampleInterval" on the next line? – Juraj Jan 12 '23 at 12:11
  • dachu darshan, Replace `//do some action here` with a `Serial.println("\n");` and report if the output is separated as desired. – chux - Reinstate Monica Jan 12 '23 at 12:30
  • @chux-ReinstateMonica, how many empty lines do we need? (it is print**ln**) – Juraj Jan 12 '23 at 14:00
  • @Juraj What is needed is to see why OP's output does not lead to a "how I can split it easily". So my attempt was to engage the OP. Unfortunately the comment's lead "dachu darshan, Replace .." did not succeed. – chux - Reinstate Monica Jan 12 '23 at 14:10
  • @Juraj yes it reads at separate line, but it's inside a single variable. I want to split them ie. Sample time : 1000 to a single variable and second line to another one and third to another. – dachu darshan Jan 17 '23 at 14:07

2 Answers2

0

You can use strtok() to split the string:

// let say buffer = "Ahmed:2000:"
// using ':' as split character
char *value = strtok(buffer, ":");
// now value have the first word before ':' ----> Ahmed
value = strtok(NULL, ":");
//now value contain the second word ----> "2000" as string

Then use atoi() to convert from a string to an int:

int number = atoi(value);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

You could consider parsing each whole line with the sscanf():

Here is an example implementation for how to parse your lines using sscanf() (for simplicity, I'm ignoring the SD card, and just providing manual c-string literals):

void setup() {

  Serial.begin(9600);

  char testline_1[] = "SampleTime: 100";
  char testline_2[] = "SampleInterval: 1000";
  char testline_3[] = "Phone: 91987654331";

  // Display initial inputs

  Serial.println("===== Initial Inputs =====");
  Serial.println(testline_1);
  Serial.println(testline_2);
  Serial.println(testline_3);
  Serial.println();

  // Parse data

  int sample_time;
  int sample_interval;
  char phone_number[20];

  sscanf(testline_1, "%*s %i", &sample_time);
  sscanf(testline_2, "%*s %i", &sample_interval);
  sscanf(testline_3, "%*s %s", phone_number);

  // Display parsed data

  Serial.println("===== Parsed Data =====");
  Serial.print("sample_time     = "); Serial.println(sample_time);
  Serial.print("sample_interval = "); Serial.println(sample_interval);
  Serial.print("phone_number    = "); Serial.println(phone_number);
  Serial.println();

}

void loop() {

}

This is what is output to the Serial Monitor, demonstrating successful parsing of the input:

===== Initial Inputs =====
SampleTime: 100
SampleInterval: 1000
Phone: 91987654331

===== Parsed Data =====
sample_time     = 100
sample_interval = 1000
phone_number    = 91987654331

tedz2usa
  • 76
  • 3
  • This is really great when I want to parse string which is in the code itself. What about incoming string from the sd card stored in a single variable called buffer. – dachu darshan Jan 16 '23 at 04:08
  • Absolutely. If you can read the SD card line by line, reading the line into a buffer, then you can use the strategy above to parse the line. Here looks to be an example of someone who successfully got reading an SD card line by line into a buffer: https://stackoverflow.com/a/35508454/2529368 It appears the key is to call `readStringUntil('\n')` . (Note: the answer I linked uses a loop to read every line. Since you have specific parsing requirements for each line, I would recommend you manually call readStringUntil('\n') 3 times, and handle each line specifically). – tedz2usa Jan 17 '23 at 18:15