简体   繁体   English

多次使用strtok_s

[英]Using strtok_s more than once

I am using strtok_s to pull out a value from a C-String based on user input. 我正在使用strtok_s根据用户输入从C字符串中提取值。 However, I need to be able to call this function more than one time. 但是,我需要能够多次调用此函数。 Currently when I do it has a problem going through strtok_s. 目前,当我这样做时,通过strtok_s遇到了问题。 I believe because it is still pointing to a different location and not starting again. 我相信是因为它仍然指向另一个位置,并且不会重新开始。 ListOfCoeffs is my c-string, which is just a c-string list that has doubles. ListOfCoeffs是我的c字符串,这只是一个具有双精度的c字符串列表。 Degree is the int value that is passed into the function from the user. 度是从用户传递到函数的int值。 Is there any way to "reset" strtok so that it will allow me to use this function more than one time without shutting the program down? 有什么方法可以“重置” strtok,以便它可以使我多次使用此功能而无需关闭程序? Apologies for poor style using strtok_s, I am not familiar with it at all. 对于使用strtok_s的糟糕风格表示歉意,我一点都不熟悉。

char *  pch;
double coeffValue;
char * context;



pch = strtok_s(listOfCoeffs, " ", &context);

if (degree == 0)
{
    // DO NOTHING
}

else
{
    for (int i = 0; i < degree; i++)
    {
        pch = strtok_s(NULL, " ", &context);
    }
}


coeffValue = atof(pch);
return coeffValue;

The strtok family of functions destructively modify the input string while tokenizing it. strtok系列函数在标记输入字符串时会破坏性地修改输入字符串。 If you want to potentially start over at the beginning, you need to copy the string before passing it to strtok . 如果您想从头开始,则需要先复制字符串,然后再将其传递给strtok

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM