strtok()帮助 [英] strtok ( ) help
问题描述
我正在使用strtok()捕获输入行。在我打电话给
" splitCommand"后,我再次调用strtok()来获取下一行。 Strtok()
返回NULL(但文件中有更多...)。在'splitCommands''进入图片之前,这并没有发生
。问题是在
splitCommands()以某种方式修改指针,但我必须调用
函数。有没有办法复制它或什么?
/ *这里是我的代码* /
char * lineOfScript; < br $>
const char * delim =" \ n" ;;
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //找到开始
在脚本中放置
lineOfScript = strtok(lineOfScript,delim); //跳过一行
lineOfScript = strtok(NULL,delim); //得到下一行...现在我有一个
命令
splitCommand(lineOfScript); //这可能是我的指针得到的地方
搞砸了......
lineOfScript = strtok(NULL,delim); //得到下一行,但是strtok返回
NULL
//将命令拆分成单独的单词。
//存储全局数组中的单词
int splitCommand(char * command){
const char * delimeters =" " ;;
int i = 0;
g_UserCommands [0] = strtok(command,"");
while(g_UserCommands [i]!= NULL&& i< 5){
// printf("%s",g_UserCommands [i]); //用于调试...
i + = 1;
g_UserCommands [i] = strtok(NULL,"");
}
i = 0;
返回1;
}
I''m using strtok( ) to capture lines of input. After I call
"splitCommand", I call strtok( ) again to get the next line. Strtok( )
returns NULL (but there is more in the file...). That didn''t happen
before ''splitCommands'' entered the picture. The problem is in
splitCommands( ) somehow modifying the pointer, but I HAVE to call that
function. Is there a way to make a copy of it or something ?
/* HERE IS MY CODE */
char * lineOfScript;
const char * delim = "\n";
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //find starting
place in script
lineOfScript = strtok(lineOfScript,delim); //skip a line
lineOfScript = strtok(NULL,delim); //get next line... now I have a
command
splitCommand(lineOfScript); //this is probably where my pointer gets
messed up...
lineOfScript = strtok(NULL,delim); //get next line, but strtok returns
NULL
//Split up command into seperate words.
//Store words in global array
int splitCommand(char * command){
const char * delimeters = " ";
int i = 0;
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands[i] != NULL && i < 5){
//printf("%s", g_UserCommands[i]);//for debugging...
i+=1;
g_UserCommands[i] = strtok(NULL, " ");
}
i=0;
return 1;
}
推荐答案
ern写道:
我正在使用strtok()来捕获输入行。在我调用
splitCommand后,我再次调用strtok()来获取下一行。 Strtok()
返回NULL(但文件中有更多...)。在splitCommands进入图片之前,这并没有发生。问题在于splitCommands()以某种方式修改指针,但我必须调用那个
函数。有没有办法复制它或什么?
/ *这里是我的代码* /
char * lineOfScript;
const char * delim = " \\\
";
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //在脚本中找到起始位置
lineOfScript = strtok(lineOfScript,delim); //跳过一行
lineOfScript = strtok(NULL,delim); //得到下一行...现在我有一个
命令
splitCommand(lineOfScript); //这可能是我的指针弄乱的地方......
lineOfScript = strtok(NULL,delim); //获取下一行,但是strtok返回
NULL
//将命令分成单独的单词。
//在全局数组中存储单词
int splitCommand(char * command){
const char * delimeters =" " ;;
int i = 0;
g_UserCommands [0] = strtok(command,"");
while(g_UserCommands [i]!= NULL&& i< ; 5){
// printf("%s",g_UserCommands [i]); //用于调试...
i + = 1;
g_UserCommands [i] = strtok( NULL,");
}
i = 0;
返回1;
}
I''m using strtok( ) to capture lines of input. After I call
"splitCommand", I call strtok( ) again to get the next line. Strtok( )
returns NULL (but there is more in the file...). That didn''t happen
before ''splitCommands'' entered the picture. The problem is in
splitCommands( ) somehow modifying the pointer, but I HAVE to call that
function. Is there a way to make a copy of it or something ?
/* HERE IS MY CODE */
char * lineOfScript;
const char * delim = "\n";
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //find starting
place in script
lineOfScript = strtok(lineOfScript,delim); //skip a line
lineOfScript = strtok(NULL,delim); //get next line... now I have a
command
splitCommand(lineOfScript); //this is probably where my pointer gets
messed up...
lineOfScript = strtok(NULL,delim); //get next line, but strtok returns
NULL
//Split up command into seperate words.
//Store words in global array
int splitCommand(char * command){
const char * delimeters = " ";
int i = 0;
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands[i] != NULL && i < 5){
//printf("%s", g_UserCommands[i]);//for debugging...
i+=1;
g_UserCommands[i] = strtok(NULL, " ");
}
i=0;
return 1;
}
In splitCommand()你开始一个新的strtok()" session",所以当
你返回并接下来调用它时,实际上它会从''
剩下的地方搜索''命令''。这是
splitCommand()的局部变量,并且没有告诉我们曾经有什么内容
splitCommand()返回(它实际上/不是/还有)。
C按值传递/所有/参数(甚至指针),即副本
用于被调用函数。函数返回后,
对象不再存在。试图访问它会调用Undefined
行为。
干杯
弗拉基米尔
-
我的电子邮件地址是真的,我看了。
In splitCommand() you start a new strtok() "session", so when
you return and invoke it next it actually searches from where it
left off in ''command''. This was a local variable to
splitCommand(), and there''s no telling what''s there once
splitCommand() returns (it actually /is not/ there anymore).
C passes /all/ parameters by value (even pointers), i.e. a copy
is made for the called function. After the function returns the
object ceases to exist. Trying to access it invokes Undefined
Behaviour.
Cheers
Vladimir
--
My e-mail address is real, and I read it.
Vladimir S. Oka写道:
Vladimir S. Oka wrote:
ern写道:
我正在使用strtok()来捕获输入行。在我调用
splitCommand后,我再次调用strtok()来获取下一行。 Strtok()
返回NULL(但文件中有更多...)。在splitCommands进入图片之前,这并没有发生。问题在于splitCommands()以某种方式修改指针,但我必须调用那个
函数。有没有办法复制它或什么?
/ *这里是我的代码* /
char * lineOfScript;
const char * delim = " \\\
";
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //在脚本中找到起始位置
lineOfScript = strtok(lineOfScript,delim); //跳过一行
lineOfScript = strtok(NULL,delim); //得到下一行...现在我有一个
命令
splitCommand(lineOfScript); //这可能是我的指针弄乱的地方......
lineOfScript = strtok(NULL,delim); //获取下一行,但是strtok返回
NULL
//将命令分成单独的单词。
//在全局数组中存储单词
int splitCommand(char * command){
const char * delimeters =" " ;;
int i = 0;
g_UserCommands [0] = strtok(command,"");
while(g_UserCommands [i]!= NULL&& i< ; 5){
// printf("%s",g_UserCommands [i]); //用于调试...
i + = 1;
g_UserCommands [i] = strtok( NULL,");
}
i = 0;
返回1;
}
I''m using strtok( ) to capture lines of input. After I call
"splitCommand", I call strtok( ) again to get the next line. Strtok( )
returns NULL (but there is more in the file...). That didn''t happen
before ''splitCommands'' entered the picture. The problem is in
splitCommands( ) somehow modifying the pointer, but I HAVE to call that
function. Is there a way to make a copy of it or something ?
/* HERE IS MY CODE */
char * lineOfScript;
const char * delim = "\n";
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //find starting
place in script
lineOfScript = strtok(lineOfScript,delim); //skip a line
lineOfScript = strtok(NULL,delim); //get next line... now I have a
command
splitCommand(lineOfScript); //this is probably where my pointer gets
messed up...
lineOfScript = strtok(NULL,delim); //get next line, but strtok returns
NULL
//Split up command into seperate words.
//Store words in global array
int splitCommand(char * command){
const char * delimeters = " ";
int i = 0;
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands[i] != NULL && i < 5){
//printf("%s", g_UserCommands[i]);//for debugging...
i+=1;
g_UserCommands[i] = strtok(NULL, " ");
}
i=0;
return 1;
}
在splitCommand()中你开始一个新的strtok()会话,所以当你返回
并接下来调用它时,它实际上是从它停止的位置搜索
''command''。这是splitCommand()的局部变量,并且没有
告诉splitCommand()返回时它是什么(它实际上/不是/
那里)。
In splitCommand() you start a new strtok() "session", so when you return
and invoke it next it actually searches from where it left off in
''command''. This was a local variable to splitCommand(), and there''s no
telling what''s there once splitCommand() returns (it actually /is not/
there anymore).
有关strtok()如何工作的详细说明,请参阅系统帮助/手册。 C标准在7.21.5.8中对此进行了描述。
我很久以前就用它来禁止释义。
干杯
弗拉基米尔
-
我的电子邮件地址是真的,我看了。
Consult your system help/manuals for a detailed description of
how strtok() works. The C Standard describes it in 7.21.5.8.
I''ve last used it too long ago to dare paraphrase either.
Cheers
Vladimir
--
My e-mail address is real, and I read it.
ern写道:
我正在使用strtok()来捕获输入行。在我调用
splitCommand后,我再次调用strtok()来获取下一行。 Strtok(
)返回NULL(但文件中有更多...)。在splitCommands进入图片之前,这并没有发生。问题在于splitCommands()以某种方式修改指针,但我必须调用
该函数。有没有办法复制它或什么?
/ *这里是我的代码* /
char * lineOfScript;
const char * delim = " \\\
";
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //找到
脚本中的起始位置
lineOfScript = strtok(lineOfScript,delim); //跳过一行
lineOfScript = strtok(NULL,delim); //得到下一行...现在我有一个
命令
splitCommand(lineOfScript); //这可能是我的指针弄乱的地方......
lineOfScript = strtok(NULL,delim); //获取下一行,但是strtok
返回NULL
//将命令分成单独的单词。
//在全局数组中存储单词
int splitCommand(char * command){
const char * delimeters =" " ;;
int i = 0;
g_UserCommands [0] = strtok(command,"");
while(g_UserCommands [i]!= NULL&& i< ; 5){
// printf("%s",g_UserCommands [i]); //用于调试...
i + = 1;
g_UserCommands [i] = strtok( NULL,");
}
i = 0;
返回1;
}
I''m using strtok( ) to capture lines of input. After I call
"splitCommand", I call strtok( ) again to get the next line. Strtok(
) returns NULL (but there is more in the file...). That didn''t happen
before ''splitCommands'' entered the picture. The problem is in
splitCommands( ) somehow modifying the pointer, but I HAVE to call
that function. Is there a way to make a copy of it or something ?
/* HERE IS MY CODE */
char * lineOfScript;
const char * delim = "\n";
lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //find
starting place in script
lineOfScript = strtok(lineOfScript,delim); //skip a line
lineOfScript = strtok(NULL,delim); //get next line... now I have a
command
splitCommand(lineOfScript); //this is probably where my pointer gets
messed up...
lineOfScript = strtok(NULL,delim); //get next line, but strtok
returns NULL
//Split up command into seperate words.
//Store words in global array
int splitCommand(char * command){
const char * delimeters = " ";
int i = 0;
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands[i] != NULL && i < 5){
//printf("%s", g_UserCommands[i]);//for debugging...
i+=1;
g_UserCommands[i] = strtok(NULL, " ");
}
i=0;
return 1;
}
strtok (lineOfScript,delim);
初次调用后,strtok()必须记住你要求的数据
to解析。要做到这一点,它会复制任何指向
的lineOfScript,并将其存储在一些内部缓冲区中[它维护,并且你不能直接使用
访问]。
strtok(NULL,delim);
当你再次调用它时,将NULL作为第一个参数传递,它只是继续
从以前停止的地方进行解析 - 即,它继续解析内部缓冲区的内部缓冲区,由最初指向的lineOfScript设置。
strtok(" BOO,delim);
现在,如果再次使用非NULL初始参数调用它,它会忘记以前存储的任何
数据/正在处理并重置其内部缓冲区,以便将您刚刚传入的任何数据复制到一个BOO的副本中。在这种情况下。所以,
无论你还没有解析 - 最初由lineOfScript引用 -
现在已经丢失并被遗忘。
>
底线,你不能做你想做的事情......
p = strtok(p1,p2);
while(strtok(NULL,p2))
{
p3 = strtok(p4,...);
< br $> b $ b ...
}
-
============= ===================================== =============
试图减少信号上的不必要的噪音......
免责声明:
>
我提供的任何评论/代码可能= = = 100%便携,也不是
语义正确[读 - ''不是100%迂腐正确'']。
我不太关心这一点,而且我认为这是与大多数访客相同的
。但是,请放心,任何
''必要''(?)更正几乎肯定会出现v.soon
[读 - ''加上他们认为合适的噪音,一个学生将会很快就会接受
。'
警告:请务必阅读标签。没有旁边的细节
过滤器提供。远离儿童。不要点燃。
====================================== ============ =============
strtok(lineOfScript,delim);
After the initial call, strtok() has to ''remember'' the data you''ve asked it
to parse. To do that here, it makes a copy of whatever lineOfScript pointed
to, and stores it in some internal buffer [that it maintains, and you can''t
directly access].
strtok(NULL,delim);
When you call it again, passing NULL as the first param, it simply continues
parsing from wherever it previously left off - i.e., it continues to parse
its internal buffer as set by whatever lineOfScript originally pointed to.
strtok("BOO",delim);
Now, if you call it again with a non-NULL initial param, it forgets whatever
data it was previously storing/working on and resets its internal buffer to
whatever data you''ve just passed in - a copy of "BOO" in this case. So,
whatever you didn''t yet parse - that was originally ref''ed by lineOfScript -
is now lost and forgotten.
Bottom line, you can''t do what you''re trying to do with ...
p = strtok(p1, p2);
while(strtok(NULL, p2))
{
p3 = strtok(p4, ...);
...
}
--
================================================== =============
In an attempt to reduce ''unwanted noise'' on the ''signal'' ...
Disclaimer:
Any comment/code I contribute might =NOT= be 100% portable, nor
semantically correct [read - ''not 100% pedantically correct''].
I don''t care too much about that though, and I reckon it''s the
same with most ''visitors'' here. However, rest assured that any
''essential'' (?) corrections WILL almost certainly appear v.soon
[read - ''to add noise as they see fit, a pedant will be along
shortly''].
WARNINGS: Always read the label. No beside-the-point minutiae
filter supplied. Keep away from children. Do not ignite.
================================================== =============
这篇关于strtok()帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!