[英]Why doesn't my strcmp actually detect the smallest between 2 words?
[英]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.