Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.

Similar presentations


Presentation on theme: "1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX."— Presentation transcript:

1 1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX Shells / Processes / Shell Scripts

2 2 Line Input and Output, K&R 7.7 The standard C library equivalents to getline and putline: fgets and fputs Function fgets like getline() from a file char *fgets(char *line, int maxline, FILE *fp); Reads the next input line (including '\n' at the end) from file fp into the char array line –at most maxline-1 chars will be read –then a terminal '\0' will be added. Returns ptr to line or NULL (means EOF)

3 3 Line Input and Output /* sample code to understand fgets – similar to getline */ char *fgets(char *s, int n, FILE *iop) { register int c; register char *cs; cs = s; while (--n >0 && (c = getc(iop)) != EOF)) if ((*cs++ =c) == '\n') break; *cs = '\0'' return (c == EOF && cs == s) ? NULL : s; }

4 4 Line Input and Output Function fputs writes line to fp int fputs(char *line, FILE *fp); It returns EOF if error occurs (disk fills up?) otherwise it returns zero Can use perror to print out exact error cause (to stderr, not user screen)

5 5 Miscellaneous Functions, K&R 7.8 Look at pg. 251-253 where they are covered 7.8.1 String Operations (string.h) –Used many of these already –Examples: size_t strlen(const char *s) int strcmp(const char *s, const char *t) char *strcpy(char *s, const char *t) char *strncpy(char *s, const char *t, size_t n)

6 6 Miscellaneous functions 7.8.2 Character Processing Functions (ctype.h) –Used at least one of these already –Examples: int isalnum(int c) int isalpha (int c) int isdigit (int c) int isxdigit (int c) int isspace (int c) int tolower (int c) int toupper (int c)

7 7 Miscellaneous functions 7.8.3 Ungetc (stdio.h) –Had something like this in homework, ungetch() –Only have guarantee that you can push one char back, but that is usually enough in practice –Example: int c = getc (stdin) /* expecting an alpha character */ if (!isalpha(c)) ungetc (c, stdin) /* if not alpha - return it */

8 8 Miscellaneous functions 7.8 4 Command Execution (stdlib.h) –The function system() See pg 253 int system(const char *s); –The string s contains a UNIX system command system(“pwd”); system(“ls >fname”); system(“prog”); –Return value depends on the command executed –Learn that from man page for the command

9 9 Miscellaneous functions –In program compiled as prog, determine values for a and b through argc and argv[ ] int a, b; char command[MAXCMD]; … sprintf(command, "prog %d %d > prog.out", a, b); system(command); –System call doesn't return data from the command, but by writing > prog.out it creates an output file –Calling program can then open/read “prog.out” file

10 10 Miscellaneous Functions 7.8.5 Storage Management (stdlib.h) malloc() andfree() –Review: Why is this incorrect? for (p = head; p != NULL; p = p->next) free(p); –Review: Why must it be written like this? for (p = head; p != NULL; p = q) { q = p->next; free(p); }

11 11 Miscellaneous Functions 7.8.6 Math functions (math.h) –Need to use #include in source code –And use math library flag in execution of gcc: gcc -lm source.c –See list in appendix, mostly familiar! –Examples: double pow (double x, double y) /* Not “x**y” */

12 12 Miscellaneous Functions 7.8.7 Random number functions (stdlib.h) –Two functions: rand (void) srand (unsigned int seed) /* default value is 1 */

13 13 qsort Library function qsort prototype (stdlib.h): void qsort(void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It sorts an array of data using quick sort algorithm basea pointer to the table (an array of unknown type data) n the number of elements in the table sizethe size of each element What’s the last argument? –A pointer to a compare function for specific data type NOT the same function as qsort in K&R, pg 120

14 14 bsearch Library function bsearch prototype (stdlib.h): void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It searches for element containing key in an array of data of unknown that is already sorted in ascending order Returns: –pointer to element (if found) or –NULL (if not found) Last argument is a pointer to the compare function Should use same compare function as was used for sorting

15 15 qsort and bsearch qsort and bsearch don’t understand the data type for the elements of the table that it sorts or searches How can we tell that? –key (for bsearch only) is a type void * –base (the pointer to the table for both) is a type void * –size (the size of each element for both) is provided –Last argument is a pointer to the correct compare function with a cast of the argument list variables to void *

16 16 Example: qsort and bsearch /* compare function for integers lowest to highest, we pass a pointer to this function to qsort/bsearch */ int intcompare(int *i, int *j) { return (*i - *j); }

17 17 Example: qsort and bsearch main () { int i, *ip, *kp, a [10] = {8, 2, 9, 6, 5, 1, 3, 7, 4, 0}; qsort ( (void *) a, sizeof a/sizeof (int), sizeof(int), (int (*) (void *, void *)) intcompare); for (i = 0; i < 10; i++) printf(“%d, ”, a[i]); /* prints “0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ” */

18 18 Example: qsort and bsearch kp = &a[3]; ip = (int *) bsearch ( (void *) kp, (void *) a, sizeof a/sizeof (int), sizeof(int), (int (*) (void *, void *)) intcompare); if (ip != NULL) /* ip will be &a[3] */ printf(“found %d\n”, *ip); /* prints “found 3” */ }

19 19 UNIX Time Representation UNIX time is kept as a signed int representing the number of seconds from Jan 1, 1970 UTC That date is referred to as the UNIX epoch –Earliest representable date: 12/13/1901 –Latest representable date: 01/18/2038 UNIX will have a Y2.038K problem!

20 20 UNIX Time Representation clock_t/* elapsed processor time */ time_t/* calendar time */ struct tm {/* calendar time components */ int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year;... };

21 21 UNIX Time Representation Time functions (time.h) clock_t clock(void) /* returns processor time */ time_t time (time_t *tp) /* returns current time */ double diff_time(time_t time2, time_t time1) /* returns time difference in seconds */ struct tm *gmtime(const time_t *tp) /* returns *tp as UTC struct tm */ struct tm *localtime(const time_t *tp) /* returns *tp as local time struct tm */ char *asctime(const struct tm *tp) /* returns a string representing *tp, e.g. Sun Jan 3 13:08:42 1988\n\0 */


Download ppt "1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX."

Similar presentations


Ads by Google