advanced programming in the unix environment
DESCRIPTION
Advanced Programming in the Unix Environment. Ch 5. Standard I/O Library. Standard I/O Library. ANSI C Standard Buffering : Library handles buffer allocation and optimal buffer size Stream based I/O Different from System V STREAMS I/O system File object - PowerPoint PPT PresentationTRANSCRIPT
System programming 1
Advanced Programming in the Advanced Programming in the Unix EnvironmentUnix Environment
Ch 5. Standard I/O LibraryCh 5. Standard I/O Library
System programming 2
Standard I/O LibraryStandard I/O Library
ANSI C StandardANSI C Standard Buffering : Library handles buffer allocation and optimal buffer sizeBuffering : Library handles buffer allocation and optimal buffer size StreamStream based I/O based I/O
– Different from System V STREAMS I/O systemDifferent from System V STREAMS I/O system FileFile object object
– FILE structure: contains all information to manage the streamFILE structure: contains all information to manage the stream– E.g. file descriptor, pointer to a buffer, size of the buffer, error flag etc.E.g. file descriptor, pointer to a buffer, size of the buffer, error flag etc.
File Pointer File Pointer (FILE * type)(FILE * type)– Open a stream Open a stream gets a pointer to FILE object gets a pointer to FILE object– Passed as argument to most standard I/O functions, just like fdPassed as argument to most standard I/O functions, just like fd
Predefined file pointersPredefined file pointers– stdin, stdout, stderrstdin, stdout, stderr
System programming 3
Programming with Standard I/O LibraryProgramming with Standard I/O Library#include <stdio.h>
main(void)
{
FILE *ifp; FILE *ofp; int c;
if ( (ifp = fopen("infile", "r")) == NULL) {
fprintf(stderr,"input file fopen error");
exit(1);
}
if ( (ofp = fopen("outfile", "w")) == NULL) {
fprintf(stderr,"output fopen error");
exit(1);
}
while ((c=getc(ifp)) != EOF) {
putc(c,ofp);
}
exit(0);
}
# gcc test.c
# echo "my name is kim" > infile
# ./a.out
# cat outfile
my name is kim
#
System programming 4
Functions in Standard I/OFunctions in Standard I/O Buffer management functionsBuffer management functions
– setbuf, setvbufsetbuf, setvbuf Flushing functionFlushing function
– fflushfflush
Stream opening/closing functionsStream opening/closing functions– fopen, freopen, fdopen, fclosefopen, freopen, fdopen, fclose
Unformatted stream I/O functionsUnformatted stream I/O functions– Character I/O functionsCharacter I/O functions
getc, fgetc, getchar, ungetc, putc, fputc, putchargetc, fgetc, getchar, ungetc, putc, fputc, putchar– Line I/O functionsLine I/O functions
fgets, gets, fputs, putsfgets, gets, fputs, puts– Binary I/O functionsBinary I/O functions
fread, fwritefread, fwrite– PositioningPositioning
ftell, fseek, rewind, fgetpos, fsetposftell, fseek, rewind, fgetpos, fsetpos
Formatted stream I/O functionsFormatted stream I/O functions– printf, fprintf, sprintf, vprintf, vfprintf, vsprintf, printf, fprintf, sprintf, vprintf, vfprintf, vsprintf,
scanf, fscanf, sscanfscanf, fscanf, sscanf
System programming 5
Buffering of Standard I/OBuffering of Standard I/O
Goal of standard I/O bufferingGoal of standard I/O buffering– minimize the number of read and write system callsminimize the number of read and write system calls
– Automatic buffering for each I/O streamAutomatic buffering for each I/O stream
Buffering typesBuffering types– fully buffered (e.g. disk)fully buffered (e.g. disk)
Actual I/O takes place when the standard I/O buffer is filledActual I/O takes place when the standard I/O buffer is filled
FlushFlush : writing (to kernel space) of a standard I/O buffer : writing (to kernel space) of a standard I/O buffer
– line buffered (e.g., terminal)line buffered (e.g., terminal)I/O when a newline character is encountered on input/outputI/O when a newline character is encountered on input/output
– unbuffered (e.g., stderr)unbuffered (e.g., stderr)No buffering of charactersNo buffering of characters
System programming 6
Buffering of Standard I/OBuffering of Standard I/O
Default buffering characteristics (ANSI)Default buffering characteristics (ANSI)– stdin, stdout : fully buffered iff not interactive devicestdin, stdout : fully buffered iff not interactive device
– stderr : never fully bufferedstderr : never fully buffered
Default buffering characteristics (SVR4, 4.3+BSD)Default buffering characteristics (SVR4, 4.3+BSD)– stderr : unbufferedstderr : unbuffered
– terminal : line bufferedterminal : line buffered
– otherwise : fully bufferedotherwise : fully buffered
System programming 7
Buffer Management FunctionsBuffer Management Functions
Should be called Should be called afterafter opening stream, opening stream, beforebefore any I/O operation any I/O operation setbufsetbuf :Set buffer for stream :Set buffer for stream
– Turn off buffering if Turn off buffering if bufbuf is is NULLNULL– bufbuf should be should be BUFSIZBUFSIZ buffer or buffer or NULLNULL– BUFSIZBUFSIZ is defined in <stdio.h> is defined in <stdio.h>
setvbufsetvbuf : Select buffering types and set buffer and buffer size : Select buffering types and set buffer and buffer size– buf : buf : pointer to a buffer pointer to a buffer – mode : mode : __IOFBFIOFBF(full buffered)(full buffered)/_IOLBF/_IOLBF(line buffered)(line buffered)/_IONBF/_IONBF(no buffer)(no buffer)
– size : size : buffer sizebuffer size
– If If mode mode = = _IOFBF _IOFBF or or _IOLBF _IOLBF and and bufbuf = = NULLNULL, then standard IO library , then standard IO library automatically allocate its own buffer of the appropriate sizeautomatically allocate its own buffer of the appropriate size
void setbuf(FILE *fp, char *buf);int setvbuf(FILE *fp, char *buf, int mode, size_t size);
returns : 0 if OK, nonzero on error
System programming 8
Buffer Management FunctionsBuffer Management Functions
Function mode buf Buffer & length Type of buffring
nonnull user buf of length BUFSIZ fully or line bufferedsetbuf
NULL (no buffer) unbuffered
nonnull user buf of length size_IOFBF
NULLSystem buffer of appropriatelength
fully buffered
nonnull user buf of length size_IOLBF
NULLSystem buffer of appropriatelength
line bufferedsetvbuf
_IONBF (ignored) (no buffer) unbuffered
IIn general, let the system choose the buffer size and automatically n general, let the system choose the buffer size and automatically allocate the buffer; then the buffer will be automatically released allocate the buffer; then the buffer will be automatically released when we close the stream.when we close the stream.
System programming 9
Flushing BufferFlushing Buffer
Pass any unwritten data for the stream to be passed to the Pass any unwritten data for the stream to be passed to the kernelkernel
Flush all output streams if Flush all output streams if fpfp is is NULLNULL
int fflush(FILE *fp); returns : 0 if OK, nonzero on error
System programming 10
Opening and Closing a StreamOpening and Closing a Stream
fopenfopen : Opens a specified file : Opens a specified file freopenfreopen : Opens a specified file on a specified stream after : Opens a specified file on a specified stream after
closing stream. Typically used to open a file as a predefined stream closing stream. Typically used to open a file as a predefined stream ((stdin, stdout, stderrstdin, stdout, stderr))
fdopenfdopen : Associate a stdio stream with an existing file descriptor. : Associate a stdio stream with an existing file descriptor. Typically used with pipe or communication channelsTypically used with pipe or communication channels
fclosefclose : Close open stream. Flush output data, discard input : Close open stream. Flush output data, discard input data.data.
FILE *fopen(const char *pathname, const char *type);FILE *freopen(const char *pathname,const char *type,
FILE *fp);FILE *fdopen(int filedes, const char *type);
returns : file pointer if OK, NULL on errorint fclose(FILE *fp);
returns : 0 if OK, EOF on error
System programming 11
Opening and Closing a StreamOpening and Closing a Stream
type Description
r,rb w,wb a,ab r+,r+b,rb+ w+,w+b,wb+ a+,a+b,ab+
open for reading truncate to 0 length or create for writing append; open for writing at the of file, or create for writing open for reading and writing truncate to 0 length or create for reading and writing open or create for reading and writing at end of file
Restriction r w a r+ w+ a+
file must already exist previous contents of file discarded
o
o
o
o
stream can be read stream can be written stream can be written only at end
o
o
o o
o o
o o
o o o
System programming 12
Opening and Closing a StreamOpening and Closing a Stream typetype argument : Concatenation of open mode characters argument : Concatenation of open mode characters
– ““r” r” :: OOpen for readingpen for reading– ““w” : w” : TTruncate to 0 length or create for writingruncate to 0 length or create for writing– ““a” a” : A: Append, open for writing or create for writingppend, open for writing or create for writing– ““+” : Open for reading and writing+” : Open for reading and writing– ““b” : Bb” : Binary /text option. No effect in Unixinary /text option. No effect in Unix
Standard I/O library cannot specify the created file’s permission. Standard I/O library cannot specify the created file’s permission. Default permission(0666) is applied.Default permission(0666) is applied.
Affected by umaskAffected by umask– Suppose a file is created via fopen(“test.txt”, “w”) and umask=0002, then Suppose a file is created via fopen(“test.txt”, “w”) and umask=0002, then
the resulting permission is 0664the resulting permission is 0664
System programming 13
Reading and Writing a StreamReading and Writing a Stream
Three type of unformatted I/OThree type of unformatted I/O– Character-at-a-time I/OCharacter-at-a-time I/O
Read/Write one character at a time.Read/Write one character at a time.
– Line-at-a-time I/OLine-at-a-time I/ORRead/Write one line at a timeead/Write one line at a time
EEach line terminated with a newline character.ach line terminated with a newline character.
– BBinaryinary I/O I/OBinary file read/writeBinary file read/write
Read/Write a structure with each operationRead/Write a structure with each operation
Formatted I/OFormatted I/O
System programming 14
Reading and Writing a StreamReading and Writing a Stream
Character-at-a-time I/Oint getc(FILE *fp); int putc(int c, FILE *fp);int fgetc(FILE *fp); int fputc(int c, FILE *fp);int getchar(void); int putchar(int c);
int ungetc(int c, FILE *fp);
Line-at-a-time I/Ochar *fgets(char *buf, int n, FILE *fp);char gets(char *buf);int fputs(const char *str, FILE *fp);int puts(const char *str);
Direct I/O (binary I/O)size_t fread(void *ptr, size_t size, size_t nobj,
FILE *fp);size_t fwrite(const void *ptr, size_t size, size_t nobj,
FILE *fp);
System programming 15
Character-at-a-time InputCharacter-at-a-time Input
returns the next char as an returns the next char as an unsigned charunsigned char cast to cast to intint..
getchargetchar : equivalent to : equivalent to getc(stdin)getc(stdin) getcgetc can be implemented as a macro can be implemented as a macro
– Should not use expression with side effectsShould not use expression with side effects– Should not use Should not use getc()getc() as a function pointer as a function pointer
fgetcfgetc cannot be implemented as macro cannot be implemented as macro ungetc ungetc : push back a character to an input stream.: push back a character to an input stream.
int getc(FILE *fp);int fgetc(FILE *fp);int getchar(void);
returns : next character if OK, EOF on end of file or errorint ungetc(int c, FILE *fp);
returns : c if OK, EOF on error
System programming 16
Distinguish between EOF and errorDistinguish between EOF and error
getchar, getc, getchar, getc, fgetc fgetc returnsreturns EOF EOF both on error both on error and on end of fileand on end of file
To distinguish, we must call either To distinguish, we must call either ferrorferror or or feoffeof
int ferror(FILE *fp);
int feof(FILE *fp);
return value : non-zero if condition is true, 0 otherwise
void clearerr(FILE* fp);
clear error or end-of-file flags
System programming 17
Character-at-a-time OutputCharacter-at-a-time Output
int putc(int c, FILE *fp);int fputc(int c, FILE *fp);int putchar(int c);
returns : c if OK, EOF on error
putchar(c)putchar(c) is equivalent to is equivalent to putc(c,stdout)putc(c,stdout) putc putc can be implemented as a macro can be implemented as a macro fputcfputc cannot be implemented as a macro cannot be implemented as a macro
System programming 18
Line-at-a-Time I/OLine-at-a-Time I/O
getsgets reads from reads from stdinstdin– Replace the terminating ‘\n’ with ‘\0’Replace the terminating ‘\n’ with ‘\0’– Deprecated function. Buffer overflow can occur; Don’t use it!Deprecated function. Buffer overflow can occur; Don’t use it!
putsputs writes null terminated string writes null terminated string strstr– Replace the terminating ‘\0’ with ‘\n’Replace the terminating ‘\0’ with ‘\n’
fgetsfgets reads till newline or reads till newline or EOFEOF or reading or reading n-1n-1 characters characters– bufbuf is terminated by a null byte is terminated by a null byte
fputs fputs writes null terminated string writes null terminated string strstr– The null byte at the end is not writtenThe null byte at the end is not written
Always use Always use fgetsfgets and and fputsfputs if you don’t want to remember if you don’t want to remember differencdifferencees! (s! (need to deal with the newline at the end of each line))
char *fgets(char* buf, int n, FILE * fp);char *gets(char * buf);
returns : buf if OK, NULL on EOF or errorint fputs(const char* str, FILE *fp);int puts(const char * str);
returns : nonnegative value if OK, EOF on error
System programming 19
Direct I/O (Binary I/O)Direct I/O (Binary I/O)
Application Usage : read/write a binary array, read/write a structureApplication Usage : read/write a binary array, read/write a structure
size_t fread(void *ptr,size_t size, size_t nobj,FILE *fp);
size_t fwrite(const void *ptr, size_t size,size_t nobj,
FILE *fp);
return value: number of objects read or written
struct item {
short count;
long total;
char name [NAMESIZE];
} itemArray[10];
fwrite(itemArray, sizeof(struct item), 5, fp);
float data[10];
if (fwrite(&data[2],sizeof(float),4,fp) !=4) {...}
System programming 20
Positioning a StreamPositioning a Streamlong ftell(FILE *fp);
returns : current file position indicator (in bytes) if OK, -1L on error
int fseek(FILE *fp, long offset, int whence);
returns : 0 if OK, nonzero on error
void rewind(FILE *fp); whencewhence
– SEEK_SETSEEK_SET : Seek from the beginning of file : Seek from the beginning of file– SEEK_CURSEEK_CUR : Seek from current file position : Seek from current file position– SEEK_ENDSEEK_END : Seek from the end of file. : Seek from the end of file.
New functions with ANSI CNew functions with ANSI C– A new abstract data type, A new abstract data type, fpos_tfpos_t, records a file position, records a file position
int fgetpos(FILE *fp, fpos_t *pos);
int fsetpos(FILE *fp, const fpos_t *pos);
returns : 0 if OK, nonzero on error
System programming 21
Formatted I/OFormatted I/O
int printf(const char *format, …);
int fprintf(FILE *fp,const char *format, …);
returns : number of characters output if OK, negative value on error
int sprintf(char *buf,const char* format, …);
returns : number of characters stored in array except null character
Formatted OutputFormatted Output
int scanf(const char * format, …);
int fscanf(FILE *fp,const char *format, …);
int sscanf(const char *buf,const char* format, …);
returns : # of input items assigned, EOF if error or eof before any conversion
Formatted InputFormatted Input
System programming 22
Misc.Misc.
If If ptrptr is is NULLNULL, pathname is stored in a static area. Should copy pathname , pathname is stored in a static area. Should copy pathname before another before another tmpnamtmpnam call. call.
If If ptrptr is not is not NULLNULL, it is assumed to point to an array of at least , it is assumed to point to an array of at least L_tmpnamL_tmpnam characters (defined in <stdio.h>)characters (defined in <stdio.h>)
tmpnamtmpnam adds directory prefix (defined as adds directory prefix (defined as P_tmpdirP_tmpdir in <stdio.h>) in <stdio.h>) automatically.automatically.
tmpfiletmpfile creates a temporary file that is automatically removed when it is creates a temporary file that is automatically removed when it is closed or on program termination.closed or on program termination.
char *tmpnam(char *ptr);
returns : pointer to unique pathname
FILE *tmpfile(void);
returns : file pointer if OK, NULL on error
Temporary filesTemporary files
System programming 23
#include <stdio.h>
int main(void){ char name[L_tmpnam], line[128]; FILE *fp;
printf("%s\n", tmpnam(NULL)); /* first temp name */ tmpnam(name); /* second temp name */ printf("%s\n", name);
if ( (fp = tmpfile()) == NULL) /* create temp file */ exit(1); fputs("one line of output\n", fp); /* write to temp file */ rewind(fp); /* then read it back */ if (fgets(line, sizeof(line), fp) == NULL) exit(1); fputs(line, stdout); /* print the line we wrote */
return 0;}
Program 5.4 tmpnam and tmpfile functionsProgram 5.4 tmpnam and tmpfile functions
Misc.Misc.
To specify both directory and prefixTo specify both directory and prefix
To get file descriptorTo get file descriptor
System programming 24
char *tempnam(const char *dir, const char *prefix);
returns : pointer to unique pathname
int fileno(FILE *fp);
returns : file descriptor associated with the stream