[February , 18, 2008] Watcom C 的笔记6 Watcom C (6)

          命令行参数的获得

    标准C语言定义如下: int main(int argc,char *argv[])。

第一个字符串 argv[0] 是包含全路径的程序名。

#include <stdio.h>
int main(int argc,char *argv[])
{
 int i;
 printf("You have input %d arguments. Argv[0]=[%s]\n",argc-1,argv[0]);
 //argv[0] is file name
 for (i=1;i<argc;i++)
   {
    printf("argument %d:[%s]\n",i,argv[i]);
   }
 return 0;
}

G:\>getarg /L p oo.bak
DOS/4GW Protected Mode Run-time  Version 1.97
Copyright (c) Rational Systems, Inc. 1990-1994
You have input 3 arguments. Argv[0]=[G:\GETARG.EXE]
argument 1:[/L]
argument 2:[p]
argument 3:[oo.bak]

    修改一下,枚举文件和目录的例子:

#include <stdio.h>
#include <dos.h>
int main(int argc,char *argv[])
{
	struct find_t fileinfo;
	unsigned	rc;
	/* Display name and size of "*.c" files */
	rc=_dos_findfirst("*.*",_A_NORMAL+_A_RDONLY+_A_HIDDEN+_A_SYSTEM+_A_SUBDIR,&fileinfo);
	while ( rc == 0)
	  {
	  	//take care of this: the priority of "==" is higher than "&"
	  	if ((fileinfo.attrib & _A_SUBDIR)==0) 
	  					{printf("%14s %10ld\n",fileinfo.name,fileinfo.size);} //File
	  	else 	
	  					{printf("%14s      <DIR>\n",fileinfo.name);}					//Directory
	  	rc= _dos_findnext(&fileinfo);
	  }	

 return 0;
}

运行结果:

       RDTSC.C        429
           PCI      <DIR>
      PRACTICE      <DIR>
   GETCHAR.EXE      33236
      COMP.BAT        107
   GETCHAR.OBJ        483
       PTEST.C        667
       HELLO.C         86
          NOTE      <DIR>
     PTEST.OBJ        551
     PTEST.EXE      33256
      GETARG.C        257
     EMUFILE.C        596
      MAIN.EXE      33372
      MAIN.OBJ        504
         R.TXT       1543
    GETARG.OBJ        526
     HELLO.OBJ        409
   EMUFILE.EXE      33384
   EMUFILE.OBJ        574
     HELLO.EXE      33216
   MEMTEST.OBJ        464
     HELLO.ERR         54

						Z.t
						2008-1-19

发表评论

电子邮件地址不会被公开。 必填项已用*标注