- I have tried so many things now that I am just confused.
- I am not understanding the way char arrays work.
- I can't get the date and names extracted. And when I use just strings, I jam up the memory.
Below was my last attempt, before asking the question.
while (f.available()) {
String line = f.readStringUntil('\n');
if (line.startsWith("04-23")) {
currentDay = 1;
int str_len = line.length() + 1;
char char_array[str_len];
line.toCharArray(char_array, str_len);
Serial.println(char_array); // Everything works to here
for (int j = 0; j < 2; j++) {
b += char_array[j];
}
bMth = b.toInt (); // This should be an int of the birth month in this case: 04
}
}
Var:
format: MM-DD-YYYY,Name // this is inside the file
line = "04-23-2020,James" // This is the output from - Serial.println(char_array);
What I am trying to do is
- Extract the month/day so that I can compare it to today.
- The year so I can calculate the age.
- The name to display on OLED.
- Everything else seems to work, but I can't get my head wrapped around the char array access.
To do this in PowerShell I would do:
$line = "04-23-2020,James"
$dob = ($line.Split(","))[0]
$bMth = ($dob.Split("-"))[0] # This is not used in this example
$bDy = ($dob.Split("-"))[1] # This is not used in this example
$bYr = ($dob.Split("-"))[2]
$age = $(get-date -UFormat %Y) - $bYr
Clear-Host
Write-Host (@'
Name: {0}
Age: {1}
'@ -f $($line.Split(","))[1], $age)
In Python:
import datetime as dt
line = "04-23-2020,James"
dob = (line.split(',')[0])
bMth = (dob.split("-"))[0] # This is not used in this example
bDy = (dob.split("-"))[1] # This is not used in this example
bYr = (dob.split("-"))[2]
age = int(dt.datetime.now().year) - int(bYr)
print('Name: ',(line.split(',')[1]))
print('Age: ', age)
strtok()function. It is also described in Majenkos blog entry