簡體   English   中英

c 中的函數 strcmp 沒有檢測到符號 >?

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

所以,我只想檢測存儲在 args 中的參數列表中的符號“>”,所以我使用 strcmp() 但執行返回一個分段錯誤(核心轉儲)。 我還嘗試將第一個字符與其 ascii 值進行比較,但它也不起作用。 我還有另一種方法可以識別字符串中的 > 符號嗎?

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

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

}

編譯和鏈接:

#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);
}

在 macOS 10.14.6 上使用 Apple Clang 11.0 進入名為demo的示例並執行命令:

./demo ">"

例如,使用 csh 會導致輸出:

Argument 1 is ">".

相反,執行:

./demo > foo

結果在終端或foo中沒有輸出,因為 shell 解析>foo並從它傳遞給程序的參數中省略它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM