我想要使用getopt()程序,需要「e」或「d」選項來選擇加密或解密然後需要一個密鑰用於不管是在。我的問題是我不知道如何用getopt()來處理密鑰。我已經閱讀了許多關於getopt()的東西,以及其他許多文章。我目前得到一個浮點錯誤和核心轉儲,並收到警告消息:如何使用getopt()與非選項參數
cypher.c:在函數'main'中: cypher.c:14:3:warning:將參數2從'getopt'從不兼容的指針類型[默認啓用] /usr/include/getopt.h:152:12:note:expected'char * const *'but argument is of'char *' cypher.c:28:13 :警告:賦值使得指針中的整數沒有投射[默認啓用]
下面是實際的代碼,任何幫助表示讚賞。
include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main(int argc, char **argv[]){
int e,x;
int i=0;
int c=fgetc(stdin);
// int n=strlen(key);
int encrypt;
while((x = getopt (argc, argv, "ed")) != -1){
switch (x){
case 'e':
encrypt=1;
break;
case 'd':
encrypt=0;
break;
default:
fputs("you broke it\n",stderr);
exit(1);
}
}
char key[100];
key[100]= argv[optind++];
int n = strlen(key);
if(encrypt == 1){
while(c != EOF){
c= fgetc(stdin);
e=(c - 32 + key[i % n]) % 95 +32;
fputc(e, stdout);
i++;
}
}
else{
while(e != EOF){
c = fgetc(stdin);
c=(e - 32 -key[i % n] +3 * 95) % 95 +32;
fputc(c, stdout);
i++;
}
}
exit (0);
}
檢查'男子3 getopt' – hek2mgl
@ hek2mgl,手冊頁吮吸,如果你是一個新人,所以downvoting,太爛太... – Oz123
我在這裏沒有低估任何人。我認爲這個手冊頁確實有幫助,因爲它帶有例子和很好的解釋。它不*吸*,試試看。我也讀過它,很好。我已經明確地發佈了'man 3 ...',因爲我想表明只要輸入'man getopt'就可以給你shell程序的man頁面,這是一個不同的事情,並且行爲與C功能。快樂選項解析! :) – hek2mgl