C語言函數strtol和strtok的用法如下:
long strtol(const char *nptr, char **endptr, int base)
示例代碼:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345abc";
char *endptr;
long num = strtol(str, &endptr, 10);
if (endptr == str) {
printf("No digits were found.\n");
}
else {
printf("The number is: %ld\n", num);
printf("The next character is: %s\n", endptr);
}
return 0;
}
char *strtok(char *str, const char *delim)
示例代碼:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello,World,How,Are,You";
char *token;
token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
return 0;
}