如何从C中的字符串中提取包括null值的所有令牌



下面的代码使用strtok函数通过delimeter","从字符串中提取令牌。

前任。

#include<stdio.h>
#include <string.h>
int main() {
char string[200] = "$GNGNS,103600.01,5114.51176,N,00012.29380,W,45.6,,,V*00";
// Extract the first token
char * token = strtok(string, ",");
// loop through the string to extract all other tokens
while( token != NULL ) {
printf( " %sn", token ); //printing each token
token = strtok(NULL, ",");
}
return 0;
}

输出为

$GNGNS
103600.01
5114.51176
N
00012.29380
W
45.6
V*00

这里忽略45.6V*00之间的空值,,,。如果它为空,我如何打印所有令牌?类似:

$GNGNS
103600.01
5114.51176
N
00012.29380
W   
45.6

V*00

函数strtok不认为空令牌是有效令牌。但是,使用strchr而不是strtok:可以很容易地解决问题

#include <stdio.h>
#include <string.h>
int main()
{
const char *const string =
"$GNGNS,103600.01,5114.51176,N,00012.29380,W,45.6,,,V*00";
const char *p = string, *q;
//continue looping until there are no more ',' characters
while ( ( q = strchr( p, ',' ) ) != NULL )
{
//print token
printf( "%.*sn", (int)(q-p), p );
//make p point one character past the ',', which is the
//first character of the next token, unless the next
//token is empty
p = q + 1;
}
//print last token
printf( "%sn", p );
}

程序输出:

$GNGNS
103600.01
5114.51176
N
00012.29380
W
45.6

V*00

此解决方案还具有允许字符串为只读的优点。因此,我可以将字符串定义为字符串文字,而不是数组(不过我不必这样做(。这在strtok中是不可能的,因为该函数具有破坏性,因为它用null终止字符覆盖分隔符,这需要字符串是可写的。

我会写一些简单的东西,而不是strtok。

char **split(char **argv, int *argc, char *string, const char delimiter, int allowempty)
{
*argc = 0;
do
{
if(*string && (*string != delimiter || allowempty))
{
argv[(*argc)++] = string;
}
while(*string && *string != delimiter) string++;
if(*string) *string++ = 0;
if(!allowempty) 
while(*string && *string == delimiter) string++;
}while(*string);
return argv;
}
int main() {
char string[200] = "$GNGNS,103600.01,5114.51176,N,00012.29380,W,45.6,,,V*00";
char *args[20];
int nargs;
split(args, &nargs, string, ',', 1);
for(int x = 0; x < nargs; x++)
{
printf("ARG[%2d] = `%s`n", x, args[x]);
}
return 0;
}

https://godbolt.org/z/vKqP4hvG4

输出:

ARG[ 0] = `$GNGNS`
ARG[ 1] = `103600.01`
ARG[ 2] = `5114.51176`
ARG[ 3] = `N`
ARG[ 4] = `00012.29380`
ARG[ 5] = `W`
ARG[ 6] = `45.6`
ARG[ 7] = ``
ARG[ 8] = ``
ARG[ 9] = `V*00`

最新更新