我想要使用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