可能有人给我解释一下什么样的差异之间存在着strtok()和strsep()?它们的优点和缺点是什么?为什么我会选择一个而不是另一个.
#include <stdio.h>
#include <string.h>
int main() {
char *slogan = "together{kaliya} [namak]";
char *slow_gun = strdup(slogan);
char *token = strsep(&slow_gun, "{");
printf ("\n slow_gun: %s\n token: %s\n", slow_gun, token);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我执行它时:
$ cc -o try try_strsep.c
$ ./try
slow_gun: kaliya} [namak]
token: together
Run Code Online (Sandbox Code Playgroud)
但是,当我将char*口号改为:
char *slogan = "kalia} [namak]";
Run Code Online (Sandbox Code Playgroud)
并执行相同的程序:
$ vi try_strsep.c
$ cc -o try try_strsep.c
$ ./try
slow_gun: (null)
token: kalia} [namak]
Run Code Online (Sandbox Code Playgroud)
我的问题是,所以当我使用strsep()并且输入字符串没有我正在寻找的模式时,返回strsep()是错误的.我可以验证strsep()是否找不到模式的唯一方法是检查if (slow_gun == NUll).
如果我有char *slogan …
我想解析一个字符串,我使用strsep函数:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "Marco:Q:2F7PKC";
char *token1, *token2, *token3;
char *r = malloc(30);
strcpy(r, str);
token1 = strsep(&r, ":");
token2 = strsep(&r, ":");
token3 = strsep(&r, ":");
printf("tok1 = %s\n", token1);
printf("tok2 = %s\n", token2);
printf("tok3 = %s\n", token3);
free(r);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该函数完成了它的工作,但是如果我启动valgrind,分配的字符串char * r不会正确释放(肯定会丢失:1个块中的30个字节).
我想知道为什么以及是否有其他方法可以做同样的事情,也许没有呼叫strsep.
我打电话给valgrind valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./a.out
运行下面的代码时,我遇到了分段错误.
它应该基本上读取.csv超过3M线的文件并在之后执行其他操作(与问题无关),但在207746次迭代后它返回分段错误.如果我删除p = strsep(&line,"|");并打印整个line它将打印> 3M线.
int ReadCSV (int argc, char *argv[]){
char *line = NULL, *p;
unsigned long count = 0;
FILE *data;
if (argc < 2) return 1;
if((data = fopen(argv[1], "r")) == NULL){
printf("the CSV file cannot be open");
exit(0);
}
while (getline(&line, &len, data)>0) {
p = strsep(&line,"|");
printf("Line number: %lu \t p: %s\n", count, p);
count++;
}
free(line);
fclose(data);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想它与内存分配有关,但无法弄清楚如何修复它.
似乎使用该函数strsep查找字符串的第一个字存在指针兼容性问题。到目前为止,我一直认为char *s并且char s[]可以完全互换。但是看来它们不是。我的程序在堆栈上使用数组失败,并显示以下消息:
foo.c: In function ‘main’:
foo.c:9:21: warning: passing argument 1 of ‘strsep’ from incompatible pointer type [-Wincompatible-pointer-types]
char *sub = strsep(&s2, " ");
^
In file included from foo.c:2:0:
/usr/include/string.h:552:14: note: expected ‘char ** restrict’ but argument is of type ‘char (*)[200]’
extern char *strsep (char **__restrict __stringp,
Run Code Online (Sandbox Code Playgroud)
我不明白这个问题。该程序使用的malloc作品。
这有效:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char s1[] = "Hello world\0";
char *s2 = malloc(strlen(s1)+1);
strcpy(s2, s1); …Run Code Online (Sandbox Code Playgroud) 我查看了 2 个 C 字符串函数 strtok_r() 和 strsep(),并注意到这两个函数都会修改传入的原始字符串的位置。
是否还有其他不修改传入的原始字符串的 C 字符串函数?
在我的应用程序中,原始字符串是动态分配的,因此我希望在解析完成后释放原始字符串。
strtok_r() 的示例
int main(){
char * str = strdup("Tutorial and example");
char* token;
char* rest = str;
printf("%s\n", rest);
while ((token = strtok_r(rest, " ", &rest)))
printf("%s\n", token);
printf("\n%s\n",str);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
输出
Tutorial and example
Tutorial
and
example
Tutorial
Run Code Online (Sandbox Code Playgroud)
在最后一行,我希望 str 指向未修改的 cstring“教程和示例”。
strsep() 也会出现类似的输出。
int main(){
char * str = strdup("Tutorial and example");
char* token;
char* rest = str;
printf("%s\n", rest);
while ((token = strsep(&rest, " "))) …Run Code Online (Sandbox Code Playgroud)