c语言,如何读取逗号分隔的字符串,将逗号间的字符串分别提取出来。

2024-11-29 01:36:33
推荐回答(1个)
回答1:

先将所有的读进来存在一个字符串中,然后用字符分割函数strtok()//具体可参见API
例如:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;

result = strtok( str, delims );

while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}

以上代码的运行结果是:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"