c语言函数使用
strtok使用
函数参数
char *strtok(char *str, const char *delim)
- str -- 要被分解成一组小字符串的字符串。
- delim -- 包含分隔符的 C 字符串。
该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。
使用
token = strtok(str, s);
/* 继续获取其他的子字符串 */
while( token != NULL ) {
printf( "%s\n", token );
token = strtok(NULL, s);
}
需要注意
使用过函数后,原字符串的分隔符会被换成'\0'
参考链接
第二次调用使用null
第一次调用是分隔符的位置会被null指向,第二次调用时就会从null,即原先的分隔符的位置开始匹配。
readLine库使用
函数参数
char *readline (char *prompt)
prompt在接受输入前在命令行端显示提示文本。
优势
比正常的gets等函数多出上下键移动补全命令、自动补全等功能。(类似shell的命令行输入)
模板
/* A static variable for holding the line. */
static char *line_read = (char *)NULL;
/* Read a string, and return a pointer to it. Returns NULL on EOF. */
char *
rl_gets ()
{
/* If the buffer has already been allocated, return the memory
to the free pool. */
if (line_read)
{
free (line_read);
line_read = (char *)NULL;
}
/* Get a line from the user. */
line_read = readline ("");
/* If the line has any text in it, save it on the history. */
if (line_read && *line_read)
add_history (line_read);
return (line_read);
}
其中只有add_history过后才能提供上下键移动补全这种功能。

浙公网安备 33010602011771号