您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页命令行参数

命令行参数

来源:意榕旅游网


命令行参数:

可以通过在程序的main()函数中添加形式参数来接收程序在启动时从命令行中获得的各个命令行参数,包括:程序名称、路径、当前环境变量和用户指定参数等。命令行参数由启动程序截获并传递给main()。

一个典型的命令行例子如:

Mycopy srcFile destFile

一个典型的main()例子如:

int main( int argc, char *argv[ ] , char *envp[ ] );

argc:指示数组指针argv中包含的参数个数,该整数总是大于等于1。

argv:一个以NULL以为的字符串数组,存储用户输入的命令行参数。按照惯例,argv[0]为程序调用的命令,如c:\\mycopy.exe;argv[1]为第一个命令行参数,直到argv[argc-1];argv[argc]总是NULL。

envp:存储执行当前程序的用户环境变量

#include

int main(int argCount,char * argValue[], char * envp[])

{

FILE* srcFile = 0, *destFile =0;

int ch = 0;

int i = 0;

if (argCount != 3){

printf(\"Usage:%s src-file-name dest-file-name\\n\

}

else{

if((srcFile = fopen(argValue[1],\"r\")) == 0){

printf(\"Can not open source file\\\"%s\\\"!\

}else{

if((destFile = fopen(argValue[2],\"w\")) ==0){

printf(\"Can not open destination file\\\"%s\\\"!\

}else{

while((ch = fgetc(srcFile))!= EOF)

fputc(ch,destFile);

printf(\"Successful to copy a file!\\n\");

fclose(srcFile);

fclose(destFile);

printf(\"%d command line parameters are got in \\n\

printf(\"All command line parameters are list here:\\n\");

while(envp[i]!=NULL){

printf(\"%s\\n\

i++;

}

i = 0;

program

printf(\"The variable set is list here:\\n\");

while(envp[i]!=NULL){

printf(\"%s\\n\

}

}

return 1;

}

i++;

}

return 0;

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务