Bdf*_*dfy 0 c parsing tab-delimited
如何安全解析tab-delimiter字符串?例如:test\tbla-bla-bla\t2332?
strtok()是一个标准函数,用于解析具有任意分隔符的字符串.但是,它不是线程安全的.您选择的C库可能具有线程安全的变体.
另一种符合标准的方式(只是编写了这个,它没有经过测试):
#include <string.h>
#include <stdio.h>
int main()
{
char string[] = "foo\tbar\tbaz";
char * start = string;
char * end;
while ( ( end = strchr( start, '\t' ) ) != NULL )
{
// %s prints a number of characters, * takes number from stack
// (your token is not zero-terminated!)
printf( "%.*s\n", end - start, start );
start = end + 1;
}
// start points to last token, zero-terminated
printf( "%s", start );
return 0;
}
Run Code Online (Sandbox Code Playgroud)