IO Redirection and Command Line Arguments PowerPoint PPT Presentation

presentation player overlay
1 / 10
About This Presentation
Transcript and Presenter's Notes

Title: IO Redirection and Command Line Arguments


1
I/O Redirection and Command Line Arguments
2
Controlling the Command Line
  • Output redirection
  • printf and fprintf(stdout) send output to a
    logical file, commonly known as the standard
    output or stdout.
  • When a program is run in the Unix environment,
    the logical file stdout is by default associated
    with the screen being viewed by the person who
    started the program.
  • The redirection operator, gt, may be used on the
    command line to cause the standard output to be
    directed to a file.

3
Controlling the Command Line
  • Output redirection
  • p3.o gt results.txt
  • A file created using redirection may be
    subsequently viewed using the more command, the
    cat command, or edited using a text editor.

4
Controlling the Command Line
  • Input redirection
  • Like stdout, stdin may also be redirected using
    the redirection operator, lt
  • p3.o lt data.dat
  • When data is read from stdin via scanf() or
    fscanf( ), the input will be read from a file
    named data.dat

5
Controlling the Command Line
  • We can redirect both stdin and stdout
  • p3.o lt data.dat gt results.txt

6
Command Line Arguments
  • Command line arguments
  • It is often useful to pass arguments to a program
    via the command line. e.g.
  • gcc p3.c Wall o p3.o
  • In this case, there are five command line
    arguments.

7
Controlling the Command Line
  • Printing Command line arguments
  • When a program is started from the command
    line, the character strings (separated by spaces)
    comprising the program name and the remaining
    arguments are copied by the operating system into
    memory space occupied by the new program and a
    table or array of addresses is passed to the main
    function. These values can be accessed by the
    main( ) function via arguments to main()
  • int main(
  • int argc, / number of command
    line arguments /
  • char argv ) / array of addresses
    of the arguments

8
Command Line Arguments
  • Printing Command line arguments
  • Example
  • / printArgs.c /
  • int main(int argc, char argv )
  • int index 0
  • while (index lt argc)
  • printf("s\n", argvindex)
  • index 1

9
Command Line Arguments
  • When the program is invoked as follows
  • printArgs.o input hello 5 mydata.dat
  • printArgs.o
  • input
  • hello
  • 5
  • mydata.dat

10
Command Line Arguments
  • / demonstrates the use of command line arguments
    /
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • int main(int argc, char argv)
  • int i
  • fprintf(stdout, \nNumber of arguments
    d\n, argc)
  • if(argc lt 3)
  • fprintf(stderr, Expected s file1
    file2\n, argv0)
  • fprintf(stderr, Exiting\n)
  • exit(1)
  • fprintf(stdout, \Printing arguments\n)
  • for(i 0 i lt argc i)
  • fprintf(stdout, s\n, argvi)
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com