I’m reading The C Programming Language and trying to remember C’s standard library functions for IO and their system call equivalents. I couldn’t find a side-by-side chart online, so I created this to help me.
C stdlib | return value | header | system call | return value | header | |
---|---|---|---|---|---|---|
files | file pointer FILE *fp | stdio.h | file descriptor int td | |||
streams | stdin, stdout, stderr | stdio.h | 0, 1, 2 | |||
open | FILE *fopen(char *name, char *mode) | file pointer on success, NULL on failure | stdio.h | int open(char *name, int flags, int perms) | file descriptor on success, -1 on error | fctnl.h |
create | (fopen creates file if it doesn’t exist) | int creat(char *name, int perms) | fctnl.h | |||
read | int getc(FILE *fp); getchar = getc(stdin) | next char, EOF on end of file or error | stdio.h | int read(int fd, char *buf, int n) | number bytes read, 0 on EOF, -1 on error | stdio.h |
read | char *fgets(char *line, int maxline); gets = fgets(stdin) | *line on success, NULL on EOF or error | stdio.h | |||
write | int putc(int c, FILE *fp) | char written on success, EOF on error | stdio.h | int write(int fd, char *buf, int n) | number bytes written, 0 on EOF, -1 on error | stdio.h |
write | int fputs(char *line, FILE *fp); puts = fputs(stout) | non-negative int on success, EOF on error | stdio.h | |||
close | int fclose(FILE *fp) | 0 on success | stdio.h | int close(int fd) | 0 on success | unistd.h |
perms | r, w, a | |||||
flags | O_RDONLY, O_WRONLY, O_RDWR | fctnl.h | ||||
BUFSIZ | stdio.h |