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

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC 216 Introduction to Computer Systems Lecture 6 Strings, cont. & Structures and Unions & Input/Output Jan Plane & Pete Keleher {jplane, pete}@cs.umd.edu Administrivia Read Reek, Chapter 10: Structures and Unions and Reek, Chapter 15: Input/Output CMSC 216 - Wood, Sussman, Herman 2
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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 – can use constants EXIT_SUCCESS and EXIT_FAILURE instead of 0/1 9 Standard I/O Library stdio.h contains prototypes and constants for I/O routines Most I/O is stream-based and buffered to make things more efficient – line buffered – fully buffered – unbuffered Can use the function fflush() to flush buffers immediately 10
Image of page 5

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

View Full Document Right Arrow Icon
Types of streams
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern