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过后才能提供上下键移动补全这种功能。
本文作者:心比天高xzh
本文链接:https://www.cnblogs.com/xzh-personal-issue/p/17202753.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步