Unformatted text preview:

1CMSC 212 – S05 (lect 7)Announcementsz Program #1B– Due week from todayz Reading– Chapter 15 (today)• skip 15.10.1 and 15.10.2 (scanf)– Chapter 8 & 9 (next Tuesday)• skip 8.1.4• skip 9.32CMSC 212 – S05 (lect 7)Project Notesz Start Early! - It's a long projectz Helpful functions– int strncmp(char *s1, char *s2, int max);– int strcpy(char *s1, char *s2);– int atoi(char *);z Suggestions–Start simple• write assemble without labels• write assemble for only add and move– Use disasemble to check output of assemble3CMSC 212 – S05 (lect 7)I/Oz void perror(char *message);– print out the most recent error encountered by a library– prints message: <error description>–Example: • We tried to do something with the file foo.c and it didn't exist• perror("foo.c") would print foo.c: File Not Foundz Terminating execution– void exit(int status);– ends the program at that spot (no need to return from main)– status is available to other programs to indicate why the program ended• exit (0) is typical for no error• exit(-1) or some other negative value for an error4CMSC 212 – S05 (lect 7)Standard I/O Libraryz #include <stdio.h>– includes prototypes for standard I/O routinesz Most I/O is stream based– buffered to allow efficient operations • mostly to disks or networks– interactive I/O is normally flushed at useful points• printf("foo\n"); /* \n causes a flush */z FILE type– used to describe an active I/O connection– three default ones supplied on entry to main:• stdin - an input stream for default input (often keybaord)• stdout - default output stream (often terminal window) • stderr - default output stream for errors5CMSC 212 – S05 (lect 7)Opening Filesz FILE *fopen(char *name, char *mode)• name specifies the name of the file to open• mode specifies the access mode:– "r" - read (file must exist)– "w" - write (write, existing file deleted)– "a" - append (write, existing file left alone)– Check return code:• A Null return implies and error• use perror(…) to report the cause of the errorz Example:FILE *fp;fp = fopen("foo", "r");if (!fp) {perror("foo");exit(-1);}6CMSC 212 – S05 (lect 7)Operations on FILEz int fclose(FILE *fp);– close a file (flushing any output if needed)– on success, returns 0• CAN FAIL!: On AFS (e.g., WAM) files not written to server until close.z Character I/O– int fgetc(FILE *stream);• return the next character from input (as an int)• returns EOF on end of file– EOF doesn't fit into a char– check for EOF before assigning result to char – int getchar(void);• like fgetc but reads from stdin.7CMSC 212 – S05 (lect 7)Character I/O (cont.)z int fputc(int character, FILE *stream);– write character to the destination named in stream– return EOF on failure, 0 on successz int putchar(int character);– like fputc but always uses stdout.z int ungetc(int character, FILE *stream);– if you read something, and want to put it back– next read will get this character– all implementations will support at least one ungetc• may support more, but not guaranteed– moving the file pointer will discard these• see fseek, etc. later in this lecture8CMSC 212 – S05 (lect 7)Line I/Oz Often useful to read and process an entire linez char *fgets(char *buffer, int bufferSize, FILE *stream);– read a line into buffer (at most bufferSize-1 characters)• null byte added at end of buffer– reads from stream– returns NULL on error or end of line– on success returns bufferz gets: never use this function!– it leads to buffer overflow problems– class assignments will fail if you use itz int fputs(char *buffer, FILE *stream);– write a line of output to stream– returns EOF on error, non-negative value on success9CMSC 212 – S05 (lect 7)Converting Inputz When you read data it is charactersz int atoi(const char *str);– Convert characters in str into an integer– Handles sign– Ignores leading white space characters– Ignores non-numbers after integerz long atol(const char *str);– Converts characters in str into a long integer– Ignores leading white space characters– Ignores non-numbers after integerz double atof(const char *str);– Converts characters in str into a double– Ignores leading white space characters10CMSC 212 – S05 (lect 7)Printf (the full story)z int fprintf(FILE *stream, char *format, …);– sends output to streamz printf(char *format, …);– sends output to stdoutz snprintf(char *buffer, int limit, char *format, …);– sends output the the character buffer– prints at most limit-1 characters to buffer• adds null to end of stringz never! use sprintf (due to buffer overflow)11CMSC 212 – S05 (lect 7)Printf format codesz c print argument as unsigned characterz d print as decimal integerz u print as unsigned integerz x print as hex (use X for capital A-F in format)z e print in exponent form (6.02300e23)z f print in floating point formatz s print as a string (null terminated character array)z % print a % (so %% prints one %)12CMSC 212 – S05 (lect 7)Printf – Flags and Field Widthsz Flags– - left justify the field (default is right)– + for signed numbers force the + to be printed–#• For x and X ensures a leading 0x or 0Xz Precision – How many characters will be printed– Format is x.y• For int types – x is total width and y is numeric digits– Leading 0’s are added to bring width up to y• For floating points numbers – X is total width and y is digits right of decimal point13CMSC 212 – S05 (lect 7)Format code examplesABCDEFGHABC..A….%-5sABCDE..ABC….A%5.5sABCDEABCA%.5sABCDEFGH..ABC….A%5sABCDEFGHABCA%sABCDEFGHABCAFormat Code+123456789+12345-12+1%+d12345678912345-0120001%04d12345678912345-12.1…%-4d123456789.12345.-0012..0001%6.4d12345678912345-00120001%.4d123456789.12345…-12…..1%6d12345678912345-121%d12345678912345-121Format14CMSC 212 – S05 (lect 7)Binary I/Oz Write out data structures in raw format– Tends to be non-portablez size_t fread(void *buffer, size_t size, size_t count, FILE *stream);– Read into buffer, count items of size size from stream– Returns the number of items read– ret = fread(mystuff, sizeof(struct foo), 42, stdin);z size_t fwrite(void *buffer , size_t size, size_t count, FILE *stream);– Write to stream, count items of size size starting at buffer– Return the number of items written15CMSC 212 – S05 (lect 7)Other FILE Operationsz int fflush(FILE *fp);– Write any buffered


View Full Document

UMD CMSC 212 - Lecture 7

Download Lecture 7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?