标签: strtok

strtok() 与 realloc() 奇怪的行为

我有以下用 C 编写的程序:

    ...
    char *answer = NULL;
    char *pch = strtok(phrase, " "); // phrase is a string with possibly many words
    while (pch) {
        char *tmp = translate_word(pch); // returns a string based on pch
        void *ptr = realloc(answer, sizeof(answer) + sizeof(tmp) + 1000); // allocate space to answer
        if (!ptr) // If realloc fails
             return -1;
        strcat(answer, tmp); // append tmp to answer
        pch = strtok(NULL, " "); // find next word
    }
    ...

Run Code Online (Sandbox Code Playgroud)

问题是 strtok() 表现出奇怪的行为,它返回一个词,该词不存在于 …

c strtok realloc

0
推荐指数
1
解决办法
104
查看次数

为什么我的将字符串拆分为标记的 C 程序不打印任何内容

我有 C 代码,用于使用该strtok()函数将示例字符串拆分为标记。我似乎已经遵循了书中的所有内容,但我的 CLion 编译器不会打印各个字符串标记并使用此代码退出脚本Process finished with exit code -1073741819 (0xC0000005)。下面是我尝试分割字符串的方法,帮助我调试问题。

//declare and assign the sentence I need to split
char * sentence ="Cat,Dog,Lion";
//declare and assign the delimiter 
char * delim=",";
//get the first token
char * token = strtok(sentence, delim);
while(token!= NULL){
     //print a single string token
      printf("%s",token);
      //reset the loop for the iteration to continue
      token = strtok(NULL,delim);
    }
Run Code Online (Sandbox Code Playgroud)

c strtok

0
推荐指数
1
解决办法
100
查看次数

C - 有时分隔符之间没有值时如何用分隔符分割字符串?

我试图按如下方式分割字符串: 1.97E+13,1965.10.30,12:47:01 AM,39.1,23,greece,,,,,10,4.8,4.6,4.6,4.8,4.6,4.7

我使用strtokand Give,作为分隔符,但由于某些逗号之间没有值,因此出现分段错误。

将空值分配给连续逗号的正确方法是什么?

c split c-strings strtok segmentation-fault

0
推荐指数
1
解决办法
145
查看次数

strtok在C中的局部变量上

我在C中调用一个外部方法时遇到了一个有趣的问题,该方法尝试strtok通过引用传递给它的本地(主)字符串.如果我在main中strtok字符串,它按预期工作,但如果我调用外部方法,它会失败并出现segfault.Valgrind在访问strtok结果的第一个元素时的输出:

Address 0xfffffffffeffebc0 is not stack'd, malloc'd or (recently) free'd
Run Code Online (Sandbox Code Playgroud)

--- test.c ---

extern void tt(char* x);

main(argc, argv) 
    int argc;
    char *argv[];
{
    char line[5000];
    // if I uncomment the following two lines and comment above it works as expected
    // char * line;
    // line = malloc(5000);
    strcpy(line, "hello world");
    printf("%d\n", sizeof(line));
    tt(line);
}
Run Code Online (Sandbox Code Playgroud)

--- test2.c ---

void tt(char* l) {
    char* x = strtok(l, " \t");
    printf("%p\n", x);
    printf("%c\n", x[0]);
}
Run Code Online (Sandbox Code Playgroud)

编译

gcc -c test2.c -o test2.o …
Run Code Online (Sandbox Code Playgroud)

c strtok

-1
推荐指数
1
解决办法
296
查看次数

strtok导致段错误,但不是在单步执行代码时

我是C的新手,我正在尝试将日期/时间字符串拆分为单独的变量.但是,当我逐行遍历gdb中的代码时,它可以工作,但是,当我让它正常运行而没有断点时它会出错并且我看不清楚原因.

以下是代码:

char * dateTimeString = "2011/04/16 00:00";
char dateVar[11];
char timeVar[6];

if (splitTimeAndDateString(dateVar, timeVar, dateTimeString))
{
    exit(1);
}

printf("Date: %s\tTime: %s\n", dateVar, timeVar);
Run Code Online (Sandbox Code Playgroud)

以下是功能

int splitTimeAndDateString(char date[11], char time[6], char * dateString)
{
    char *token;
    token = strtok(dateString, " ");
    int i = 0;
    while (token != NULL)
    {
        if (i == 0)
        {
            strcpy(date, token);
        }
        else if (i == 1)
        {
            strcpy(time, token);
        }
        else
        {
            printf("Overrun date time string\n");
            return 1;
        }
        token = strtok(NULL, " …
Run Code Online (Sandbox Code Playgroud)

c strtok

-1
推荐指数
1
解决办法
562
查看次数

Strtok用法,代码无效

我正在尝试使用strtok().以下是我写的一段代码.它不起作用,但", '"无限打印.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
char str[]="this, by the way, is a 'sample'";
char *tokens;
tokens = strtok(str, ", '");
//printf("%s\n",tokens);
//printf("%s\n", str);
while(tokens!=NULL)
{
    printf("%s\n", tokens);
    tokens = (NULL, ", '");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下是strtok()手册页中的代码,完全正常.

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf …
Run Code Online (Sandbox Code Playgroud)

c strtok

-1
推荐指数
1
解决办法
1176
查看次数

strtok()函数在C中无法正常工作

strtok()使用以下代码在C语言的循环中应用:

printf("%s",line);
printf("%d %d %d\n",atoi(strtok(line," ")),atoi(strtok(NULL," ")),atoi(strtok(NULL," ")) );
Run Code Online (Sandbox Code Playgroud)

输出是:

103 70 105 150

103 0 0

115 17 127 21

115 127 17

10 108 105 97

10 105 8

13 122 43 8

13 43 122

50 187 35 71

50 35 187
Run Code Online (Sandbox Code Playgroud)

奇数线表示使用后的线和偶数线strtok().

我不知道为什么我没有将每个数字分开

即在103 70 105 150我需要所有数字均匀分开.

c gcc strtok

-1
推荐指数
1
解决办法
455
查看次数

与strtok一起使用的C自由变量

我已经看到过类似的帖子,但是我发现了一些差异,使我误入歧途。

我有以下代码:

char * token_one = strtok(my_buffer, " ,.-");
char * token_two = strtok(NULL, " ,.-");

free(token_one);
free(token_two);
Run Code Online (Sandbox Code Playgroud)

我看到有人在帖子中说不应释放与strtok一起使用变量,但是为什么在执行此代码时却得到此信息:

free(token_one) 没有错误

free(token_two)我得到“ 无效指针

为什么我没有收到错误消息free(token_one)?处理此问题的正确方法是什么?

c pointers strtok undefined-behavior invalid-pointer

-3
推荐指数
1
解决办法
83
查看次数