以下是一个示例程序,可以在 Windows 的命令提示符中传入参数,并将其输出到屏幕上:

#include <iostream>

int main(int argc, char* argv[]) {
    std::cout << '传入的参数个数:' << argc << std::endl;
    std::cout << '传入的参数值:' << std::endl;
    
    for (int i = 0; i < argc; ++i) {
        std::cout << '参数' << i << ': ' << argv[i] << std::endl;
    }
    
    return 0;
}

编译并运行该程序后,在命令提示符中输入以下命令:

program.exe arg1 arg2 arg3

输出将会是:

传入的参数个数:4
传入的参数值:
参数0: program.exe
参数1: arg1
参数2: arg2
参数3: arg3

其中,program.exe 是程序的可执行文件名,arg1、arg2 和 arg3 是传入的参数值。


原文地址: https://www.cveoy.top/t/topic/qB3A 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录