Hi there!
Im trying to simplify reading from a SD card with some helper functions that I want to wrap inside a class later. Im having a hard time understanding the pointer (*) variables so im not really sure how to solve this (if its even possible in C). I want to stay away from the String class and only want to use Chars.
Here is my code so far:
#define CMD_LINE_BUFFERSIZE 20
#define CMDS_PER_LINE 3
#define SINGLE_CMD_LENGTH 6
char stringBuffer[CMD_LINE_BUFFERSIZE+1];
char cmdBuffer[CMDS_PER_LINE][SINGLE_CMD_LENGTH+1];
void readFileByLine(char *filename)
{
int readPos = 0;
char *cmdsDelimiter = "#";
char *subDelimiter = ":";
File theFile = SD.open(filename);
if(!theFile)
{
Serial.println("Failed to open file.");
return;
} else {
while (theFile.available())
{
char ch = theFile.read();
if(ch == '\n')
{
// CommandString complete
readPos = 0;
splitCharArray(stringBuffer,cmdsDelimiter);
Serial.println(cmdBuffer[0]);
Serial.println(cmdBuffer[1]);
Serial.println(cmdBuffer[2]);
} else {
stringBuffer[readPos] = ch;
readPos++;
}
}
theFile.close();
}
}
void splitCharArray(char *theBuffer,char *delimiter)
{
char *token = strtok(theBuffer, delimiter);
int tokenCounter = 0;
while (token != 0)
{
strcpy(cmdsBuffer[tokenCounter],token);
token = strtok(0, delimiter);
tokenCounter++;
}
}
The splitCharArray() function works but ONLY when I specify the global char cmdsBuffer inside of it. I want to pass ANY char array to the function to split the input char array into it! ![]()
To clarify a bit more what I am trying to achieve:
Lets say I have 2 Char buffers
char cmdBuffer[3][7];
char subBuffer[2][4];
And I get this C String: "A:123#B:456#C:789" from the SD Card saved in stringBuffer.
I dont want to do the whole strtok() stuff over and over again (also for reusage purposes) I want to call the function splitCharArray() and pass the appropriate buffer to it to fill it. I found that I can pass a variable by reference like this char &buffer.
So I tried this:
char *splitCharArray(char *theString,char *delimiter,char &tmpBuffer)
{
char *token = strtok(theString, delimiter);
int tokenCounter = 0;
while (token != 0)
{
strcpy(tmpBuffer[tokenCounter],token);
token = strtok(0, delimiter);
tokenCounter++;
}
return tmpBuffer;
}
But I allways get this error:
p0005_SDReadTest.ino: In function 'void readFileByLine(char*)':
p0005_SDReadTest:69: error: invalid initialization of non-const reference of type 'char&' from a temporary of type 'char ()[7]'
p0005_SDReadTest:17: error: in passing argument 3 of 'char splitCharArray(char*, char*, char&)'
p0005_SDReadTest.ino: In function 'char* splitCharArray(char*, char*, char&)':
p0005_SDReadTest:96: error: invalid types 'char[int]' for array subscript
p0005_SDReadTest:103: error: invalid conversion from 'char' to 'char*'
Pseudo Code (it would be a charm if it would work like this):
char stringBuffer[20];
char cmdBuffer[3][7];
char subBuffer[3][2][4];
stringBuffer = "A:123#B:456#C:789";
// Extract the commands from stringBuffer save it in cmdBuffer
cmdBuffer = splitCharArray(stringBuffer,"#");
for(int i=0;<sizeof(cmdBuffer);i++)
{
// Extract each command from the cmdBuffer
subBuffer[i] = splitCharArray(cmdBuffer[i],":");
}
Is there a way to dynamicaly pass a char buffer to a function in C to make reusage possible?
Hope I explained my problem correctly ![]()
Thanks in advance!