简体   繁体   中英

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). I also tried comparing the the first character with its ascii value but it didn't work either. 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:

./demo ">"

with, for example, csh, results in output:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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