Command Line Parameters - PowerPoint PPT Presentation

1 / 5
About This Presentation
Title:

Command Line Parameters

Description:

Command Line Parameters – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 6
Provided by: MikeKat5
Category:

less

Transcript and Presenter's Notes

Title: Command Line Parameters


1
Command Line Parameters
2
Passing Unix parameters to C (1)
  • Often a user wants to pass parameters into the
    program from the UNIX prompt
  • obelix3 gt progname arg1 arg2 arg3
  • This is accomplished in C using argc and argv
  • For example
  • int main (int argc, char argv )
  • / Statements go here /
  • Call this program from Unix
  • obelix 4 gt progname arg1 "second arg" arg3

array of strings (char )
3
Passing Unix parameters to C (2)
  • include ltstdio.hgt
  • int main(int argc, char argv)
  • int count
  • printf ("Program name s\n", argv 0)
  • if (argc gt 1)
  • for (count1 countltargc count)
  • printf ("Argument d
    s\n",count,argvcount)
  • else
  • puts ("No command line arguments entered.")
  • return 0

4
Passing Unix parameters to C (3)
  • Suppose we compiled the previous program to the
    executable file myargs
  • obelix5 gt myargs first "second arg" 3 4 gt
    myargs.out
  • myargs.out contains the following lines
  • Program name myargs
  • Argument 1 first
  • Argument 2 second arg
  • Argument 3 3
  • Argument 4 4

5
Passing Unix arguments to C (4)
  • / show file /
  • include ltstdio.hgt
  • int main(int argc, char argv )
  • FILE fp int k
  • if(argc !2)
  • printf("Usage s filename\n",
    argv0)
  • return 0
  • if((fpfopen(argv1, "r"))NULL)
  • printf("Cannot open file!\n")
  • return 1
  • while((kfgetc(fp))!EOF ) fputc(k, stdout)
  • fclose(fp)
  • return 0
  • Generally a main function of a C program for Unix
    checks whether the arguments are valid and prints
    simple help information.
Write a Comment
User Comments (0)
About PowerShow.com