简体   繁体   English

c 中的函数 strcmp 没有检测到符号 >?

[英]the function strcmp in c doesn't detect the symbol >?

So, I just want to detect the symbol '>' in a the list of arguments stored in args, so I use strcmp() but the execution returns me a segmentation error (core dumped).所以,我只想检测存储在 args 中的参数列表中的符号“>”,所以我使用 strcmp() 但执行返回一个分段错误(核心转储)。 I also tried comparing the the first character with its ascii value but it didn't work either.我还尝试将第一个字符与其 ascii 值进行比较,但它也不起作用。 Is there another way for me to recognize the > symbol in a string ?我还有另一种方法可以识别字符串中的 > 符号吗?

if(strcmp(argv[1], ">") == 0){

if(execlp("date", "date", NULL) == -1){
    perror("erreur exec");
    exit(EXIT_FAILURE);
}

}

Compiling and linking:编译和链接:

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


int main(int argc, char *argv[])
{
    for (int i = 0; i < argc; ++i)
        if (strcmp(argv[i], ">") == 0)
            printf("Argument %d is \">\".\n", i);
}

into an example named demo with Apple Clang 11.0 on macOS 10.14.6 and executing the command:在 macOS 10.14.6 上使用 Apple Clang 11.0 进入名为demo的示例并执行命令:

./demo ">"

with, for example, csh, results in output:例如,使用 csh 会导致输出:

Argument 1 is ">".

In contrast, executing:相反,执行:

./demo > foo

results in no output in the terminal or in foo because the shell parses > and foo and omits them from the arguments it passes to the program.结果在终端或foo中没有输出,因为 shell 解析>foo并从它传递给程序的参数中省略它们。

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

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