怎么用命令型参数进行运行

怎么用命令型参数进行运行,尝试用命令提示符,但好像参数无法读取,是哪里出现问题了吗?

img

img


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

int main(int argc, char *argv[])
{
    int sum = 0;
    while(--argc)
    {
        if(argc != 0)
        {
            sum += atoi(argv[argc]);
        }
    }
    printf("Total:%d\n", sum);
    system("pause");
    return 0;
}

供参考:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
    FILE *fp;
    int sum = 0, tmp;
    char buff[16];
    if(argc > 1){
        fp = fopen(argv[argc-1], "r");
        if (!fp){
            printf("%s open fail!\n", argv[argc-1]);
            system("pause");
            return 1;
        }
        while ((fgets(buff,sizeof(buff),fp)) != NULL){
            if (sscanf(buff,"%d",&tmp) == 1)
                 sum += tmp;
            memset(buff,0,sizeof(buff));
        }
        fclose(fp);
        printf("Total:%d\n", sum);
    }
    system("pause");
    return 0;
}

img

直接 后面传参即可,需要注意 实际参数 argv[0] 是 exe程序名,argv[1] 往后是你实际的参数

-- main.exe 是 argv[0]    hello 是 argv[1] ,  world 是 argv[2]  
main.exe  hello  world