【问题标题】:Trying to output into a text file the encrypted or decrypted file尝试将加密或解密的文件输出到文本文件中
【发布时间】:2015-03-01 23:41:19
【问题描述】:

while循环出了点问题,但我真的不明白为什么。它应该将文件的加密或解密结果打印到带有参数的输出文件中(progname.exe -e input.txt)

问题是没有使用加密或解密代码创建输出文件

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int c;
    FILE *fp;
    FILE *output;

    if (argc < 2) {
        exit(EXIT_FAILURE);
    } else if ((fp = fopen(argv[1], "r")) == NULL) {
        exit(EXIT_FAILURE);
    }

    if (strncmp (argv[1], "-d", 2) == 0) {

        output = fopen (argv[5], "w+");

        while (fscanf(fp,"%x",&c) != EOF)  {
            c = c >> 2;
            c = c-200;
            printf("%c", c);
        }
    }

    if (strncmp (argv[1], "-e", 2) == 0) {

        output = fopen (argv[5], "w+");

        while ((c = getc(fp)) != EOF) {
            c = c+200;
            c = c << 2;
            printf("%04x ", c);
        }
    }

    putchar('\n');

    return EXIT_SUCCESS;
}

【问题讨论】:

  • 它当前正在打开一个文本文件,它是加密还是解密它取决于用户
  • 问题是没有使用加密或解密代码创建的输出文件
  • 你没有打开文件 fp.您在 argv[5] 中查找输出文件的名称。这可能远远超出您提供的参数数量。我猜你的意思是 argv[2]。然后修复 argc 检查。你永远不会写入输出。您写入标准输出(屏幕)。等
  • 当我在我的代码中实现参数时,它停止打印加密或解密的代码
  • 糟糕,看到你打开了 fp。但是你使用 argv[1] 来获得它的名字。同时argv[1]应该是“-e”或者“-d”。你的程序应该有 3 个参数 (argc == 4)。 argv[1] 是“-e”或“-d”,argv[2] 是 fp 的名称,argv[3] 是输出的名称。先解决这个问题。要写入输出文件,请使用printf(output, ...)。首先尝试简单地将 fp 复制到输出中而不更改文本并使其正常工作。

标签: c encryption


【解决方案1】:

程序参数的顺序/数量似乎没有意义。

程序似乎期望输入文件作为它的第一个参数 (argv[1]):

} else if ((fp = fopen(argv[1], "r")) == NULL) {
    exit(EXIT_FAILURE);
}

如果第一个参数不是现有文件,程序将退出而不写入任何输出。

稍后将相同的参数与"-d""-e" 进行比较以检查解密/加密:

if (strncmp (argv[1], "-d", 2) == 0) {

可能您打算在输入文件名argv[2] 之后使用程序参数?如果该参数不是-d-e,程序将不会写入任何输出文件。

另外将第五个参数argv[5]作为输出文件名。如果这不是有效的文件名,也不会创建任何输出文件。

【讨论】:

    【解决方案2】:

    代码中的问题主要是命令行参数使用不当。

    argc 包含参数计数,因此如果您有 ls -a -l file,那么 argc 将包含值 4,有了这些信息,您可以使用 switch 轻松解析程序内部的命令行参数大小写关于传递的参数数量,或者您可以查看命令行解析器库。

    • getopt() / getopt_long()(来自 POSIX C 库的#include &lt;unistd.h&gt;),可以解决简单的参数解析任务,bash 内置的 getopt 是基于 GNU libc 的 Getopt。
    • Argp(来自 GNU C 库的#include &lt;argp.h&gt;)。

    这是一个使用您的代码的开关盒示例,

    它可以接受命令行参数行。

    输出到文件:

    prog.exe -e input.txt output.txt
    prog.exe -d output.txt input2.txt 
    

    输出到标准输出:

    prog.exe -e input.txt
    prog.exe -d output.txt
    

    对于其他参数,它将打印无效参数错误并返回。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        int c;
        FILE *input = NULL;
        FILE *output = NULL;
        int encryption = 1;   // Flag to set encryption/decryption.
    
        switch ( argc ) {
        case 3:
            if ( strchr(argv[1],'-') != NULL ) {
                if ( argv[1][1] == 'e' ) encryption = 1;
                else if ( argv[1][1] == 'd' ) encryption = 0;
                else {
                    fprintf(stderr,"Error: invalid Option %s\n",argv[1]);
                    exit(EXIT_FAILURE);
                }
                if ((input = fopen( argv[2], "r")) == NULL) {
                    fprintf(stderr,"Error: Cannot Open %s\n",argv[2]);
                    exit(EXIT_FAILURE);
                }
                output = stdout;
            } else {
                fprintf(stderr,"Error: Invalid arguments\n");
                exit(EXIT_FAILURE);
            }
            break;
        case 4:
            if ( strchr(argv[1],'-') == NULL || 
                 strchr(argv[2],'-') != NULL ||
                 strchr(argv[3],'-') != NULL ) {
                fprintf(stderr,"Error: invalid argument\n");
                exit(EXIT_FAILURE);
            } else {
                if ( argv[1][1] == 'e' ) encryption = 1;
                else if ( argv[1][1] == 'd' ) encryption = 0;
                else {
                    fprintf(stderr,"Error: invalid Option %s\n",argv[1]);
                    exit(EXIT_FAILURE);
                }
                if ((input = fopen( argv[2], "r")) == NULL) {
                    fprintf(stderr,"Error: Cannot Open %s\n",argv[1]);
                    exit(EXIT_FAILURE);
                }
                if ((output = fopen( argv[3], "w+")) == NULL) {
                    fprintf(stderr,"Error: Cannot Open %s\n",argv[3]);
                    exit(EXIT_FAILURE);
                }
           }
           break;
        default:
            fprintf(stderr,"Error: Invalid arguments\n");
            exit(EXIT_FAILURE);
        }
    
        // Do encryption and decryption based on the flag.
        if ( encryption ) {
            while ( fscanf(input,"%c",(char*)&c) == 1 ) { // Do encryption
                c = c + 200;
                c = c << 2;
                fprintf(output,"%x ", c);
            }
        } else {
            while ( fscanf(input,"%x ",&c) == 1 )  { // Do decryption
                c = c >> 2;
                c = c - 200;
                fprintf(output,"%c", c);
            }
        }
    
        return EXIT_SUCCESS;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多