I've seen numerous examples of this in the forum and implemented the code that is common in the solutions, however, I can't figure out what is wrong with my implementation. I have a text file that has these contents:
ref
ywDheGHD1PVZaky7Ul8T
acc
uBwqUqi6ymXc7N8kayTp
exp
2500
The goal of the function is to update the text file with the token that is being passed to it. This is the function call in the setup:
char kind[] = "ref";
char newToken[] = "Ac6H7pMlAcUxZsLCOmqx";
updateTokens(kind, newToken);
and here is the function:
void updateTokens(char *which, char *theToken) {
String keepToken = "";
int tokenPos = 0;
char buf[5];
dataFile = sd.open("tokens.txt", FILE_READ);
if(!dataFile){
Serial.println(F("File not found"));
return;
} else{
if(strncmp(which, "ref", 3) == 0){
Serial.println("keeping the access token");
while(dataFile.available()){
dataFile.read(buf, 3);
Serial.print("buffer: "); Serial.println(buf);
if(strncmp(buf, "acc", 3) == 0){
tokenPos = dataFile.position() + 1;
Serial.print("found changing token at pos: "); Serial.println(tokenPos);
break;
}
}
}
else if(strncmp(which, "acc", 3) == 0){
Serial.println("keeping the refresh token");
while(dataFile.available()){
dataFile.read(buf, 5);
if(strncmp(buf, "ref", 3) == 0){
tokenPos = dataFile.position() + 1;
Serial.print("found changing token at pos: "); Serial.println(tokenPos);
break;
}
}
}
char r;
dataFile.seek(tokenPos);
r = dataFile.read();
keepToken += r;
keepToken += dataFile.readStringUntil('\r');
Serial.print("keeper token: "); Serial.println(keepToken);
}
dataFile.close();
sd.remove("tokens.txt");
delay(10);
Serial.println("time to write the file");
dataFile = sd.open("tokens.txt", FILE_WRITE);
if(!dataFile){
Serial.println(F("File not created"));
return;
} else{
if(strncmp(which, "ref", 3) == 0){
Serial.println("writing new refresh token");
dataFile.println("ref");
dataFile.println(String(theToken));
dataFile.println();
dataFile.println("acc");
dataFile.println(keepToken);
dataFile.println();
dataFile.println("exp");
dataFile.println();
}
else if(strncmp(which, "acc", 3) == 0){
Serial.println("writing new access token");
dataFile.println("ref");
dataFile.println(keepToken);
dataFile.println();
dataFile.println("acc");
dataFile.println(String(theToken));
dataFile.println();
dataFile.println("exp");
dataFile.println();
}
}
dataFile.close();
}
And this is the output from the Serial (note there is a character after the ?, it just doesn't show up). I would have anticipated it would go through the file character by character and make a buffer of 5 from that point then eventually find the text and trigger the if statement. Neither of those things seem to be happening.
22:28:38.049 -> keeping the access token
22:28:38.049 -> buffer: ref?
22:28:38.049 -> buffer:
22:28:38.049 -> y?
22:28:38.049 -> buffer: wDh?
22:28:38.049 -> buffer: eGH?
22:28:38.049 -> buffer: D1P?
22:28:38.049 -> buffer: VZa?
22:28:38.049 -> buffer: ky7?
22:28:38.049 -> buffer: Ul8?
22:28:38.049 -> buffer: T
22:28:38.049 -> ?
22:28:38.049 -> buffer:
22:28:38.049 -> a?
22:28:38.049 -> buffer: cc?
22:28:38.049 -> buffer:
22:28:38.049 -> uB?
22:28:38.049 -> buffer: wqU?
22:28:38.049 -> buffer: qi6?
22:28:38.049 -> buffer: ymX?
22:28:38.049 -> buffer: c7N?
22:28:38.049 -> buffer: 8ka?
22:28:38.049 -> buffer: yTp?
22:28:38.049 -> buffer:
22:28:38.049 -> ?
22:28:38.049 -> buffer:
22:28:38.049 -> ex?
22:28:38.049 -> buffer: p
22:28:38.049 -> ?
22:28:38.049 -> buffer: 250?
22:28:38.049 -> buffer: 0
22:28:38.049 -> ?
22:28:38.049 -> keeper token: ref