CSC320 chapter6 - Standard I/O library Defined by Dennis...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Standard I/O library Defined by Dennis Ritchie in 1975 An ANSI C standard Easy to use and portable Details handled: Buffer allocation, optimal-sized I/O portions, better interface, etc.
Image of page 1

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

View Full Document Right Arrow Icon
2 Buffering in Standard I/O Standard I/O functions use buffered I/O to reduce the number of system calls. Write(1, buf, 6) /* all output streams are flushed */ /* fflush causes any unwritten data for the stream to be passed to the kernel */
Image of page 2
3 Buffering in Standard I/O, cont. Goal Use the minimum number of read and write calls. Types 1) Fully Buffered Actual I/O occurs when the buffer is filled up. A buffer is automatically allocated when the first- time I/O is performed on a stream. flush: standard I/O lib vs terminal driver
Image of page 3

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

View Full Document Right Arrow Icon
4 Buffering in Standard I/O, cont. 2) Line Buffered Perform I/O when a newline char is encountered! usually for terminals. Warnings The filling of a fixed buffer could trigger I/O. The flushing of all line-buffered outputs if input is requested . 3) Unbuffered Expect to output asap, e.g. using write() e.g., stderr
Image of page 4
5 boxshadowdwn ANSI C Requirements Fully buffered for stdin and stdout. Standard error is never fully buffered. #include <stdio.h> int fflush(FILE *fp); All output streams are flushed if fp == NULL Buffering in Standard I/O, cont.
Image of page 5

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

View Full Document Right Arrow Icon
#include <stdio.h> void setbuf(FILE *fp, char *buf); Full/line buffering (terminal) if buf is not NULL (BUFSIZ) Turn buffereing on or off. To enable buffering, buf must point to a buffer of length BUFSIZ, defined in <stdio.h> #define BUFSIZ 1024 (<stdio.h>) void setvbuf(FILE *fp, char *buf, int mode, size_t size); mode: _IOFBF: Fully Buffered _IOLBF: Line Buffered _IONBF: Unbuffered (the buf and size arguments are ignored) They must be called before any operation is performed on the streams! Buffering in Standard I/O, cont.
Image of page 6
7 mode buf len type setbuf non-null BUFSIZ FB/LB NULL NB setvbuf FB non-null any size FB FB NULL st_blksize FB LB non-null any size LB LB NULL st_blksize LB NB ignored no buffered NB Buffering in Standard I/O, cont.
Image of page 7

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

View Full Document Right Arrow Icon
8 UNIX I/O vs Standard I/O
Image of page 8
9 UNIX I/O vs Standard I/O, cont. Each system call has analogous procedure calls from the standard I/O library: System call : Standard I/O call : open fopen close fclose read/write getchar/putchar getc/putc fgetc/fputc fread/fwrite gets/puts fgets/fputs scanf/printf fscanf/fprintf lseek fseek
Image of page 9

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

View Full Document Right Arrow Icon
10 UNIX I/O vs Standard I/O, cont. Difference from File I/O square4 File Pointers vs File Descriptors; fopen vs open - When a file is opened/created, a stream is associated with the file. - FILE object File descriptor, buffer size, # of remaining chars, an error flag ... square4 stdin, stdout, stderr defined in <stdio.h> STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO
Image of page 10
11 Files in C The pointers are assigned when opening the file and then used for reading from it or writing to it, and finally closing it.
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern