c语言指令文件命名,C语言--在命令行输入文件名字并打印文件内容

C语言编程中,经常遇到main函数中argc和argv[]这两个参数。argc是argument count的缩写,即参数的个数;argv是argument vector的缩写,即参数列表。argv[0]是程序本身的名字,argv[1]是在命令行中输入的第一个程序的参数,argv[argc]是NULL,如下所示:

#include "stdio.h"

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

{

printf ("the argc value is %d \n", argc);

int i;

for (i = 0; i <= argc; i++){

printf ("the argv[%d] value is %s \n", i, argv[i]);

}

return 0;

}

#将上述代码编译为test可执行文件,在命令行输入如下内容

/*

./test arg_1 arg_2

*/

#执行结果如下:

/*

the argc value is 3

the argv[0] value is ./test_c_0

the argv[1] value is arg_1

the argv[2] value is arg_2

the argv[3] value is (null)

*/

搞清楚了argc和argv[],我们就可以使用两者通过命令行向程序传送将要处理的文件名参数,代码如下。

#include "stdio.h"

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

{

FILE *fp;

int c;

fp = fopen( argv[1], "r");

while ( (c = fgetc(fp)) != EOF){

printf ("%c", c);

}

fclose(fp);

return 0;

}

原文:http://blog.csdn.net/vernice/article/details/46421245

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值