我想让我的程序使用 getchar 的命令行参数来编码消息。我的问题是 getchar 只关注程序执行后我输入的内容。我怎样才能让它读取命令行参数呢?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int pin, charin, charout;
// this verifies that a key was given at for the first argument
if (atoi(argv[1]) == 0){
printf("ERROR, no key was found..");
return 0;
}
else {
srand(atoi(argv[1]));
pin = rand() % 27;
}
while( (charin=getchar()) != EOF){
charout = charin + pin;
putchar(charout);
}
}
最佳答案
getchar to read from command line arguments
这就是问题所在 - 如果您要获取命令行参数,则不能使用 getchar
来获取:使用 argc
来获取参数和 argv 的计数[]
获取特定参数。
我认为您在此程序中根本不需要 getchar
。
这些也是很好的建议,请看一下关于参数处理的部分:
encode program using getchar from command line argument and putchar to send to decode
我认为您正在尝试编写一个行为如下的程序:
程序 pin 消息
然后它将在消息
上使用某种凯撒密码来混淆输出。是这样吗?
如果是这样,那么您还需要获取argv[2]
并使用printf
输出结果。
关于c - getchar 从命令行参数中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33428673/