21
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

Standard I/O Library

  • Upload
    rue

  • View
    21

  • Download
    0

Embed Size (px)

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 Presentation

Citation preview

Page 1: Standard I/O Library

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Positioning a Stream Three ways to position a stream

ftell and fseek ftello and fseeko fgetpos and fsetpos

Page 16: Standard I/O Library

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

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

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

Miscint fileno(FILE *stream); Get a file descriptor from a

specified stream

Page 20: Standard I/O Library

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

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