与“>”比较时,while 循环条件永远不会计算为 true。
我正在使用 while 循环来检测“>”的存在数组中的符号。循环条件永远不会计算为 true。当我显示数组的内容时,“>”不过,符号本身就存在。在我的代码中,这不是第一次调用 strtok。这也只是总代码的一小部分。要被 strtok 分解的字符串是“ls > testfile.txt”。任何帮助将不胜感激。
while (userArgs[k] != ">") {
k++;
userArgs[k] = strtok(NULL, " ");
if(userArgs[k] == ">") {
break;
}
if (userArgs[k] == NULL){
break;
}
}
I am using a while loop to detect the presence of a ">" symbol in an array. The loop conditional never evaluates to true. When I show the contents of the array, the ">" symbol is there by itself though. In my code, this isn't the first call to strtok. This is also only a snippet of the total code. The string to be broken up by strtok is "ls > testfile.txt". Any help would be appreciated.
while (userArgs[k] != ">") {
k++;
userArgs[k] = strtok(NULL, " ");
if(userArgs[k] == ">") {
break;
}
if (userArgs[k] == NULL){
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
strtok
返回一个char *
所以我猜你正在尝试比较字符串。您无法像 C 中那样比较字符串,请使用
strcmp
。你现在所做的是比较地址,绝对不是你想要的。strtok
returns achar *
so I guess you're trying to compare strings.You can't compare strings like that in C, use
strcmp
. What you are doing now is comparing addresses, definitely not what you want.IF userArgs 是
char
类型,那么您正在将字符串与字符常量进行比较while (userArgs[k] != ">")
和if(userArgs [k] == ">")
所以实际上是将二进制程序文件中字符串的地址与字符值进行比较。请注意,">"
是一个字符串常量,由存储该字符串的某个地址表示。'>'
是一个字符常量,并且有一个值。IF
userArgs
是char *
那么您正在比较程序堆栈或堆中的userArgs[i]
位置的地址(如果是动态分配的)存储在程序二进制文件中并由操作系统加载到内存中的">"
字符串的地址。这些内存位置会随着时间的推移而变化,并且与其内容无关。要比较内存的内容,您必须使用while (strcmp (userArgs[k], ">") != 0)
。或者为了避免调用strcmp
您可能需要执行以下while ((userArgs[k][0] != '>') && (userArgs[k][1 ] == '\0')
上述示例需要根据您的需要进行更改。
IF userArgs is of type
char
then you are comparing a string with a character constantwhile (userArgs[k] != ">")
andif(userArgs[k] == ">")
So actually the address of the strings in the binary program file is compared to the character value. Note that the">"
is a string constant and is represented by some address where the string is stored'>'
is a character constant and has a value.IF
userArgs
ischar *
then you are comparing the address of theuserArgs[i]
location in the program stack or heap (if dynamically allocated) with the address of">"
string stored in the program binary and loaded by the OS on the memory. These memory location will vary from time to time and has no relation with the contents of it. To compare the contents of the memories you have to usewhile (strcmp (userArgs[k], ">") != 0)
. OR to avoid callingstrcmp
you might want to do the followingwhile ((userArgs[k][0] != '>') && (userArgs[k][1] == '\0')
The above examples would require change as per your needs.
使用
'>'
而不是">"
。第一个是字符,第二个是字符串 - 或者实际上是指向字符的指针,并且比较指针显然不是您想要的。Use
'>'
instead of">"
. The first one is a character and the second one is a string - or actually a pointer to a character, and comparing pointers clearly isn't what you want.您正在比较地址 userArgs[k] 是否与“<”的堆栈地址相同。但事实并非如此。您需要匹配
userArgs[k][0] == '>' // 注意单引号。
但如果 '>' 则这是错误的前面没有空格。You are comparing if the address userArgs[k] is the same as the stack address for "<". This is never the case. You would need to match
userArgs[k][0] == '>' // note the single quote.
but this wrong if the '>' is not preceded by a space.