w7 - CSC209: Miscellaneous C Stuff. Processes Kante Easley...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSC209: Miscellaneous C Stuff. Processes Kante Easley University of Toronto June 24, 2009 Kante Easley CSC209 CSC209: Error Checking Most system calls return -1 if an error occurs Some return NULL errno is a global variable that holds the numeric code of the last error Set to 0 at process creation time. A successful system call doesnt affect the value of errno An unsuccessful system call overwrites the value in errno Always check the return value of system calls Kante Easley CSC209 CSC209: perror In stdio.h . void perror(char *str); Displays str, then a colon (:) then an english description of the last system error as defined in errno.h Protocol for checking errors Check system calls for erronous return value call perror() and do the appropriate thing Often, the appropriate thing will be to exit(1). Kante Easley CSC209 CSC209: Binary I/O Recall that fgets reads characters. By contrast, fread and fwrite operate on bytes size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); read nmemb * size butes into memory at ptr size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); write nmemb * size bytes from ptr to the file pointer stream Kante Easley CSC209 CSC209: Binary I/O It doesnt matter what the bytes contain! /* write an integer to the file */ int num = 21, n; FILE *fp; if((fp = fopen("/tmp/should_be_unique.txt", "w")) == NU die_perror(); n = fwrite(&num, sizeof(num), 1, fp); /* write a struct to the file */ struct rec { char name[20]; int num; } r; r.num = 42; strncpy(r.name, "koala", 20); n = fwrite(&r, sizeof(r), 1, fp); Kante Easley CSC209 CSC209: Binary I/O...
View Full Document

Page1 / 19

w7 - CSC209: Miscellaneous C Stuff. Processes Kante Easley...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online