standard i/o library
DESCRIPTION
Standard I/O Library. Streams and FILE objects Three types of buffering Open a stream read/write a stream Positioning a stream Formatted I/O Temporary files. Streams and FILE Objects. Unbuffered I/O File Descriptor driven - CH 3 3 pre-defined descriptors STDIN_FILENO STDOUT_FILENO - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/1.jpg)
Standard I/O Library Streams and FILE objects Three types of buffering Open a stream read/write a stream Positioning a stream Formatted I/O Temporary files
![Page 2: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/2.jpg)
Streams and FILE Objects Unbuffered I/O
File Descriptor driven - CH 3 3 pre-defined descriptors
STDIN_FILENO STDOUT_FILENO STDERR_FILENO
Some system calls open creat lseek read write
![Page 3: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/3.jpg)
Streams and FILE Objects Standard I/O
File stream driven - FILE* Associate a file with a stream and
operate on the stream 3 pre-defined streams
stdin stdout stderr
![Page 4: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/4.jpg)
Buffering Goal of buffering is to minimize the
number of read and write calls. Three types of buffering
Fully buffered – Block Buffered Line buffered Unbuffered
ANSI C requirements stderr must never be fully buffered stdin and stdout are fully buffered if the
device they use is not interactive
![Page 5: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/5.jpg)
Buffering On most UNIX/Linux systems
stderr is always unbuffered Streams for terminals are line
buffered All other streams are fully buffered
![Page 6: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/6.jpg)
Setting Buffer Type setbufvoid setbuf(FILE *stream, char *buf);
Enable or disable buffering on a stream
If buf is NULL buffering is disabled Buffer must be of size BUFSIZ as defined
in <stdio.h>
![Page 7: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/7.jpg)
Setting Buffer Type setvbufint setvbuf(FILE *stream, char *buf, int mode , size_t size);
Can set all 3 types of buffering depending on value of mode
_IOFBF - Fully buffered _IOLBF - Line buffered _IONBF - Non buffered
If buf is NULL, system will create its own buffer automatically
buf is ignored if mode is _IONBF setbuf(fp, NULL) is the same as
setvbuf(fp, buf, _IONBF, size)
![Page 8: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/8.jpg)
Opening a StreamFILE *fopen(const char *path, const char *mode);FILE *freopen(const char *path, const char *mode, FILE *stream);FILE *fdopen(int fildes, const char *mode); fopen opens the file given by path. freopen opens a file on a specified
stream, closing the stream first if its already open
fdopen opens a stream from a file descriptor. Useful for streams that don’t have a regular file such a pipes
![Page 9: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/9.jpg)
Opening a Stream mode
r or rb w or wb a or ab r+ or r+b or rb+ w+ or w+b or wb+ a+ or a+b or ab+
![Page 10: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/10.jpg)
Reading and Writing a Stream
Three ways to read and write One character at a time One line at a time Direct (binary) I/O
Character at a timeint getc(FILE *stream);int fgetc(FILE *stream);int getchar(void);
![Page 11: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/11.jpg)
Reading and Writing a Stream
Handling errorsint feof(FILE *stream);int ferror(FILE *stream);void clearerr(FILE *stream); “Unreading”
We can put a single character back into the streamint ungetc(int c, FILE *stream);
![Page 12: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/12.jpg)
Reading and Writing a Stream
int putc(int c, FILE *stream);int fputc(int c, FILE *stream);int putchar(int c); Returns c if ok, EOF on error
getc and putc may be more efficient than fgetc and fputc because macros do not have the overhead of calling a function
![Page 13: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/13.jpg)
Reading and Writing a Stream
Line at a time I/O Readchar *gets(char *s);char *fgets(char *s, int size, FILE
*stream); Writeint fputs(const char *s, FILE *stream);int puts(const char *s);
![Page 14: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/14.jpg)
Reading and Writing a Stream
Direct (binary) I/Osize_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
Read nmemb items of size size from stream and store them at the location pointed to by ptr
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); Write nmemb items of size size to stream from
the location pointed to by ptr Useful for reading/writing arrays and structs Returns number of items written
![Page 15: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/15.jpg)
Positioning a Stream Three ways to position a stream
ftell and fseek ftello and fseeko fgetpos and fsetpos
![Page 16: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/16.jpg)
Positioning a Streamlong ftell(FILE *stream);
Returns current offset if ok, or -1L on error
int fseek(FILE *stream, long offset, int whence); whence is the same as in ch 3 Returns 0 if ok, non zero on error
void rewind(FILE *stream); Same as fseek(fp, 0L, SEEK_SET)
![Page 17: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/17.jpg)
Positioning a Streamoff_t ftello(FILE *stream);
Returns offset if ok, (off_t)-1 on errorint fseeko(FILE *stream, off_t offset, int whence);
whence is the same as ch 3 Returns 0 if ok, non zero on error
int fgetpos(FILE *stream, fpos_t *pos);int fsetpos(FILE *stream, fpos_t *pos);
Both return 0 if ok, non zero on error
![Page 18: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/18.jpg)
Formatted I/O Outputint printf(const char *format, ...);int fprintf(FILE *stream, const char *format, ...);int sprintf(char *str, const char *format, ...);int snprintf(char *str, size_t size, const char *format, ...); Inputint scanf(const char *format, ...);int fscanf(FILE *stream, const char *format, ...);int sscanf(const char *str, const char *format, ...);
![Page 19: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/19.jpg)
Miscint fileno(FILE *stream); Get a file descriptor from a
specified stream
![Page 20: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/20.jpg)
Temporary Fileschar *tmpnam(char *s);
Generates a valid random filenamechar *tempnam(const char *dir, const char *pfx);
Generates a valid random filename in the specified directory with the specified (at most) 5 character prefix
Best to avoid these two because finding the filename and creating the file are not atomic operations!
![Page 21: Standard I/O Library](https://reader035.vdocuments.us/reader035/viewer/2022062410/56816005550346895dcf06da/html5/thumbnails/21.jpg)
Temporary FilesFILE *tmpfile(void);
Generates a randomly named temp file and opens it for read/write
File is automatically removed for us when program ends
int mkstemp(char *template); template must have the last 6 characters as XXXXXX.
These will be replaced with unique characters. template can not be constant because it will be
modified. Instead, it should be a character array. File not removed automatically. We must unlink it
ourselves