Presentation is loading. Please wait.

Presentation is loading. Please wait.

Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes

Similar presentations


Presentation on theme: "Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes"— Presentation transcript:

1 Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes
Formatted IO File IO System Calls System interface to obtain services from the OS, include many subsystems Memory Scheduler File/Storage System Inter-process Communication and Network, etc A popular subsystem: File system File descriptors Low level IO File Management Examples

2 Standard IO and Pipes stdin, stdout Numbered file descriptors Pipes
cmd < inputfile cmd > outputfile Numbered file descriptors 0: stdin 1: stdout 2: stderr … Pipes cmd1 | cmd2 | cmd3 IO Redirection cmd1 < inputfile > outputfile cmd1 | cmd2 2>&1 | cmd3 (**: not for csh)

3 Formatted IO Output: printf Input: scanf
Arguments: a format string, followed by the arguments printf (char *fmt, arg1, arg2, …) scanf (char *fmt, arg1, arg2, …) Arguments for scanf have to the memory addresses

4 Formatting Output goes to stdout Input comes from stdin
Format String: regular string + conversion specification Start of a format specification: % Width, precision, adjustment:

5 String-based input String-based input
int sscanf (char *line, char *fmt, arg1, arg2, …); Except %s, which tends to be confusing for sscanf, the others are very useful.

6 Variable length arguments
int printf(char *fmt, …); <stdarg.h> a new datatype: va_list Associated macro functions: va_start(ap, last) Initialize ap to be the va_list after the argument: last. va_arg(ap, int) Expand ap to an expression that has type/value that match int ap moves to the next argument in the variable list va_end(ap) Cleanup the variable argument list when done

7 File Accesses Files and file descriptors: File input/output FILE *fp;
FILE *fopen(char *name, char *mode); Name can be a long path Mode: a combination of ‘r’, ‘w’, ‘x’, or ‘a’ File input/output int getc(FILE *fp) int putc(int c, FILE *fp); These works with default stdin/stdout: getchar() putchar()

8 Line-oriented input/output
int getline(char *line, size_t n, FILE *fp) char *fgets(char *line, size_t n, FILE *fp); gets(char *line, size_t n); /* buggy, never use it */ char *fputs(char *line, FILE *fp);

9 Error handling fprintf(stderr, char *fmt, arg1, arg2, …)
exit(1): exit with non-zero status ferror(FILE *fp): test for any error on fp feof(FILE *fp) : test for end-of-file perror(char *s): print the error message, s, to stderr for the last system or library calls


Download ppt "Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes"

Similar presentations


Ads by Google