lect06 - CMSC 216 Introduction to Computer Systems Lecture...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMSC 216 Introduction to Computer Systems Lecture 6 Strings, cont. & Structures and Unions & Input/Output Jan Plane & Pete Keleher {jplane, [email protected] Administrivia • Read Reek, Chapter 10: Structures and Unions and Reek, Chapter 15: Input/Output CMSC 216 - Wood, Sussman, Herman 2 C OMMAND L INE A RGUMENTS Section 13.4, Reek 3 Command line arguments • When executing a command like " ls -l ", or " emacs puzzles.c ", the various parts of the command line are called arguments to the program • We can access these by including parameters to the main() function • These parameters are represented as strings (similar to the String args you'd use in Java's main() method). 4 Accessing command line arguments • This program (named prog ) will print out each argument, one per line – Note: the type " char * " is also used to represent strings in C - we'll learn why soon #include <stdio.h> int main(int argc, char *argv) { int i; for (i = 0; i < argc; i++) printf("Arg #%d: %s\n", i, argv[i]); return 0; } • What if we execute " ./prog -l 53 -c -d "? 5 Accessing command line args, cont. • If we execute " ./prog -l 53 -c -d "? – Output is: Arg #0: ./prog Arg #1: -l Arg #2: 53 Arg #3: -c Arg #4: -d Accessing command line arguments • This program (named prog ) will print out each argument, one per line – Note: the type " char * " is also used to represent strings in C - we'll learn why soon #include <stdio.h> int main(int argc, char *argv) { int i; for (i = 0; i < argc; i++) printf("Arg #%d: %s\n", i, argv[i]); return 0; } • What if we execute " ./prog -l 53 -c -d "? 5 Accessing command line args, cont. • If we execute " ./prog -l 53 -c -d "? – Output is: Arg #0: ./prog Arg #1: -l Arg #2: 53 Arg #3: -c Arg #4: -d 6 I NPUT /O UTPUT Chapter 15, Reek 7 Error reporting void perror(const char *message); • prints a description of the most recent error to have occurred (in a system call and some library calls), along with the message you provide (format: " message : error desc.\n") • For example, say we have an error opening a file; the call perror("Can't open filename.txt") could result in: Can't open filename.txt: No such file or directory • System knows what error occurred by setting the global variable errno (defined in errno.h ) 8 Terminating execution void exit(int status); – prototype in stdlib.h – "immediately" ends execution when called – status is viewable by the shell • exit(0); generally means OK • exit(1); or any nonzero generally means error encountered • can use " echo $? " in tcsh to see the exit status of the last program executed...
View Full Document

This note was uploaded on 01/13/2012 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 18

lect06 - CMSC 216 Introduction to Computer Systems Lecture...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online