I am writing my own strtok function. How do I make it so that it will return the remaining string as an output parameter?
Here is what I made so far.
char *mystringtokenize(char **string, char delimiter) {
static char *str = NULL;
int stringIndex = 0;
if (string != NULL) { //check if string is NULL, if its not null set str to string
str = string;
}
if (string == NULL) { //return NULL if string is empty
return NULL;
}
do { //traverse through string
if (!str[stringIndex]) { //if str at string index is null character, stop while loop
break;
}
stringIndex++;//index through string
} while (str[stringIndex] != delimiter);
str[stringIndex] = '\0'; //cut the string
char *lastToken = str; //set last token to the cut off part
return lastToken;
}
When I call it in main, and try to pass in the file that needs to be tokenized, I get a bad exception error.
int main(int argc, char const *argv[])
{
FILE *inputStream = fopen("FitBitData.csv", "r");
int index = 0;
int fitbitindex = 0;
char testline[100];
char minute[10] = "0:00:00";
FitbitData fitBitUser[1446];
if (inputStream != NULL) {
while (fgets(testline, sizeof(testline), inputStream) != NULL) {
strcpy(fitBitUser[fitbitindex].patient, mystringtokenize(testline, ','));
strcpy(fitBitUser[fitbitindex].minute, mystringtokenize(NULL, ','));
printf("%s %s\n", fitBitUser[fitbitindex].patient, fitBitUser[fitbitindex].minute);
printf("%s\n", fitBitUser[fitbitindex].patient);
fitbitindex++;
}
}
return 0;
}
For example, when I have a line Hello, World and tokenize it. It will return Hello. But If I call it again mystringtokenize(NULL, ','), it returns a bad exception error.
There are several problems with your function. The first one is that the first parameter shall have type
char *instead ofchar **.If the first parameter is a null pointer then the function always returns
NULL:So for example such a call of the function
does not extract a string.
The pointer
strwith static storage duration does not keep the last position of the extracted string between function calls.The passed string can start from the delimiter. In this case your function returns an empty string.
Pay attention to that the function
fgetscan append the new line character'\n'to entered string. To remove the new line character'\n'you can write after a call offgetsIf you are going to make your function similar to the standard C string function
strtokthen it will be useful to read the description of the function.From the C17 Standard (7.24.5.8 The strtok function)
and
That is in particularly all characters that represent delimiters are skipped until a non-delimiter character is encountered. Otherwise the function returns a null pointer. It means that for example if the passed string is
",,"and the delimiter is','then the function in its first call shall return a null pointer. Or if the original string is",,,Hello,,,"then in the first call the function shall return substring"Hello"and in a second call it shall return a null pointer.It is important to note that the function shall never return an empty string.
Thus your function can be implemented for example the following way as shown in the demonstration program below.
The program output is