使用参数从控制台运行程序

时间:2016-11-21 18:18:37

标签: c

我的问题是如何编写一个功能,我可以使用以下参数从控制台运行程序: program.exe -i input.txt -o output.txt -t 1/2/3

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

char substitute(char letter, char* cipher)
{
    int i;
    int cipher_length = strlen(cipher);
    char substitution = letter;

    for(i = 0; i < cipher_length; ++i)
        {
            if(cipher[i] == substitution)
            {
            substitution = cipher[(i%2) ? (i-1) : (i+1)];
            break;
            }
        }
    return substitution;
}

int main()
{
    char c;
    int t;
    FILE *plik = fopen( "input.txt", "rt" );
    FILE *encoded=fopen("output.txt","wt");
    char* cipher1 = "GADERYPOLUKIgaderypoluki";
    char* cipher2 = "POLITYKARENUpolitykarenu";
    char* cipher3 = "KACEMINUTOWYkaceminutowy";

    printf("Choose the cipher\n");
    printf("[1]GA-DE-RY-PO-LU-KI\n");
    printf("[2]PO-LI-TY-KA-RE-NU\n");
    printf("[3]KA-CE-MI-NU-TO-WY\n");

    scanf("%d",&t);
    while(c != EOF)
    {
        c = getc( plik );
        switch(t)
    {
    case 1:
            putc(putchar(substitute(c, cipher1)),encoded);
            break;
    case 2: putc(putchar(substitute(c, cipher2)),encoded);
            break;
    case 3: putc(putchar(substitute(c, cipher3)),encoded);
            break;
    }
}
    fclose( plik );  
    fclose(encoded);
}

我得到了这样的,但我不知道如何使用它:

    int function(int argc, char*argcv[])
    {
        int i;
        char *string,*input,*output;
        for(i=0; i<argc; i++)
          {

          }
     return 0;
    }

1 个答案:

答案 0 :(得分:0)

参数children['sort_id']=children.parent_id parents['sort_id']=parents.object_id pd.concat([parents,children]).sort_values(['sort_id', 'parent_id']).drop('sort_id', 1) 包含命令行中的参数数量。

参数int argc是一个数组,其中所有字符串都在命令行中输入。

这样,您可以将命令行参数检索为:

char *argv[]

您可以处理命令行中传递的参数:

int main(int argc, char *argv[]) {
    int i = 0;
    for (i = 0; i < argc; i++) {
        printf("parameter %d = %s\n", i, argv[i]);
    }
}

要破坏字符串,您可以使用以下代码:

int main(int argc, char *argv[]) {
    int i = 0;
    for (i = 0; i < argc; i++) {
        ...
        if (strcmp(argv[i], "-i") == 0) {
            doSomething(argv[i+1]);
        }
        ...

    }
}
相关问题