lec4_stdio

lec4_stdio - Stdio, Files and Directories Marco Gruteser 1...

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

View Full Document Right Arrow Icon
1 Stdio, Files and Directories Marco Gruteser
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Low-level File I/O Functions C Function / Macro for every system call Open Read Write Lseek Close Unbuffered I/O Every read or write call invokes system call Open files are identified by file descriptor (nonnegative integer) Shells follow this convention 0 = STDIN_FILENO 1 = STDOUT_FILENO 2 = STDERR_FILENO
Background image of page 2
3 Standard I/O Library Portable C I/O library first for Unix, now ANSI C – runs on Windows etc. Handles buffer allocation: read into large buffer, dump to OS in fixed units performs I/O in optimal-sized chunks More efficient than system calls (read, write) if data is accessed in small chunks Buffering leads to fewer system calls
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Key Functions fopen returns pointer to FILE structure ( file pointer ) pointer to buffer fclose fread fwrite fgetc / fputc fgets / fputs
Background image of page 4
5 Buffering Minimizes read() and write() operations Three buffering modes: fully buffered: I/O buffer is filled void setbuf(FILE *fp, char *buf); // BUFSIZE or NULL line-buffered: newline character void setvbuf(FILE *fp, char *buf, int mode, size_t size); unbuffered: as soon as possible void setvbuf(FILE *fp, char *buf, int mode, size_t size); int fflush(FILE *fp);
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 I/O Efficiency Difference  between Stdio / System  calls?
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

lec4_stdio - Stdio, Files and Directories Marco Gruteser 1...

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

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