Homework / Exam • HW7 due class 25

advertisement
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
1
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)
2
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;
}
3
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)
4
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)
5
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)
6
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)
if (!isalpha(c))
ungetc (c, stdin)
/* expecting an alpha character */
/* if not alpha - return it */
7
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
8
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
9
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);
}
10
Miscellaneous Functions
• 7.8.6 Math functions (math.h)
– Need to use #include <math.h> 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” */
11
Miscellaneous Functions
• 7.8.7 Random number functions (stdlib.h)
– Two functions:
rand (void)
srand (unsigned int seed) /* default value is 1 */
12
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
base
n
size
a pointer to the table (an array of unknown type data)
the number of elements in the table
the 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
13
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
14
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 *
15
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);
}
16
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, ” */
17
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” */
}
18
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:
– Latest representable date:
12/13/1901
01/18/2038
• UNIX will have a Y2.038K problem!
19
UNIX Time Representation
clock_t
time_t
struct tm {
int
int
int
int
int
int
. .
/* elapsed processor time */
/* calendar time */
/* calendar time components */
tm_sec;
tm_min;
tm_hour;
tm_mday;
tm_mon;
tm_year;
.
};
20
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 */
21
Download