c - getchar 从命令行参数中读取

标签 c command-line-interface getchar

我想让我的程序使用 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/

相关文章:

c - 如何覆盖二维数组中的第一行?

使用自定义查询将 Mysql 导出通过管道传输到另一个 mysql 服务器

ios - Flutter 构建 iOS 到底做了什么?这真的有必要吗?

c - 如何避免使用 getchar() 按下 Enter 来仅读取单个字符?

c - getchar() 循环不会无故结束

c - getchar和putchar的实际用途是什么?

c - 如何在不显式链接数学库的情况下使用带有 gcc 的 c 中的数学函数

c - 在C中将一个单词(char [])插入一个结构单词(char [])中

c++ - 如何用C++或C将控制台的内容写入文件,多个进程/exe可能会写入控制台

javascript - 使用 Node.js 执行命令行二进制文件