16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C Libraries<br />

}<br />

while (!feof(fp1))<br />

putc(getc(fp1),fp2);<br />

fclose(fp1);<br />

fclose(fp2);<br />

return SUCCESS;<br />

Unbuffered I/O<br />

standard device | file descriptor<br />

stdin 0<br />

stdout 1<br />

stderr 2<br />

void close(int fd)<br />

int creat(char *name, int perms)<br />

long Lseek(int fd, long offset, int origin);<br />

int open(char *name, int flags, int perms)<br />

int read(int fd, char *buf, int n)<br />

int write(int fd, char *buf, int n)<br />

unlink(char *name)<br />

flags<br />

O_RDONLY<br />

O_WRONLY<br />

O_RDWR<br />

open read only<br />

open write only<br />

open read & write<br />

perms<br />

0666 all read & write<br />

String Operations<br />

#include <br />

/* s and t are char * and c and n are int */<br />

strcat(s,t) concatenate t to end of s<br />

strncat(s,t,n) concatenate n characters of t to end of s<br />

strcmp(s,t) return -ve if st<br />

strncmp(s,t,n) same as strcmp but only in first n chars<br />

strcpy(s,t) copy t to s<br />

strncpy(s,t,n) copy n characters from t to s<br />

strlen(s) return length of s<br />

strchr(s,c) return pointer to first c in s<br />

strrchr(s,c) return pointer to last c in s<br />

Testing and Conversion<br />

#include <br />

/* function returns int and c is int */<br />

isalpha(c) non-zero if c is alphabetic<br />

isupper(c) non-zero if c is upper case<br />

islower(c) non-zero if c is lower case<br />

isdigit(c) non-zero if c is digit<br />

isalnum(c) non-zero if isalpha(c) or isdigit(c)<br />

isspace(c) non-zero if c is blank, tab, lf, cr, ff, vt<br />

toupper(c) return c converted to upper case<br />

tolower(c) return c converted to lower case<br />

Error handling – Stderr and Exit<br />

Output which is sent to stderr goes to <strong>the</strong> screen not down a pipeline or into an output file. Exit stops <strong>the</strong> program<br />

and returns a value back to <strong>the</strong> system.<br />

66

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!