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

来源:互联网 发布:python web 上传文件 编辑:程序博客网 时间:2024/06/06 19:35

if (fcntl(atoi(argv[1],O_RDONLY)<0)...

   在steven的 <UNIX环境编程> 这本书中,关于fcntl函数的运用中,有如下:
     #./a.out 0 < /dev/tty
         结果:read only
         #./a.out 1 >temp.foo
         结果:write only
         #./a.out 1
         结果:read write
         #./a.out 2 2>>temp.foo
         结果:write only,append
         #./a.out 5 5<>temp.foo
         结果:read write
    
   举个例子:
     (1).参数问题
         #./pro1 file1 file2>file3
       时,仅有pro1和file1传递给了pro1进程,分别作为argv[0],argv[1].
       而file2>file3由shell处理,在fork()之后,执行exec系统调用前,打开文件file3,并将文件file3的句柄复制到file2,
       并在exec时设置文件句柄file2打开.
     
     1>.所以以上执行时,#./a.out 1 >temp.foo,1是标准输出,表示STDOUT_FILENO,此时1的句柄被重定向写到一个文件中,所以
       结果正好是:write only. (temp.foo以只写方式打开)
     2>.执行#./a.out 1
       这个时候,由于0,1,2句柄都是指向你的登陆终端文件,而该终端是刻度写的,所以是read write.
     3>.执行#./a.out 2 2>>temp.foo    
       时,shell在open temp.foo时,加了个APPEND标志,以表明是追加.
     4>.同样,执行#./a.out 5 5<>temp.foo时,<为读出,>为重定向输入,所以结果为:read write.

 

 

 

 

unix下创建一个文件

vi    yan.c

 

#include <stdio.h>

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

{

     printf ("%d \n",argc);

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

      printf("%s \n", argv[i]);

 

return 0;

}

 

编译一下

gcc yan.c  -o yan.out --std = c99

 

运行一下

 

$ yan.out yan  zheng  qing
4
yan.out
yan
zheng
qing

 

 

 

 

 

 

 

 

原创粉丝点击