Advanced Programming in the UNIX Environment Hop Lee.

Slides:



Advertisements
Similar presentations
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
Advertisements

Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Text File Input and Output. Overview Text File Buffered I/O Functions fopen Function Demo of File Write and Read.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
Kernel File Interface operating systems (or programming I/O in Unix)
Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O   4. Standard I/O Library 5. Files and Directories 6. System Data.
File I/O.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
File IO and command line input CSE 2451 Rong Shi.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C Programming Lecture 12 : File Processing
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
Files A collection of related data treated as a unit. Two types Text
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
CHAPTER 5. STANDARD I/O LIBRARY System Programming 本份投影片大量參考熊博安教授的系統程式投影片 羅習五 國立中正大學資訊工程學系 EA-001 (05)
Lecture 12 CIS 208 Friday, March 3rd , 2005.
Chapter 4 File Processing
Chapter 9 – File Input Output
ECE Application Programming
Lecture 11 File input/output
TMF1414 Introduction to Programming
File I/O.
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
What you need for the 1st phase of project
CSE1320 Files in C Dr. Sajib Datta
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Input/Output and the Operating Systems
Text and Binary File Processing
File Input and Output.
Henning Schulzrinne Columbia University
Module 12 Input and Output
Standard I/O Library Implementation
File I/O & UNIX System Interface
Professor Jodi Neely-Ritz University of Florida
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Advanced Programming in the UNIX Environment Hop Lee

Ch04 Standard I/O Library Streams and FILE Objects stdin, stdout, and stderr Buffering Standard Stream I/O Efficiency Formatted I/O Implementation Details

§4.1 Streams and FILE Objects In Chapter 3 all the I/O routines centered around file descriptors. With the standard I/O library the discussion centers around streams. When we open or create a file with standard I/O library we say that we have associated a stream with the file. When we open a stream, the standard I/O function fopen returns a pointer to a FILE object.

A FILE object is normally a structure that contains all the information required by the standard I/O library to manage the stream: the fd used for actual I/O, a pointer to a buffer for the stream, the size of the buffer, a count of the number of characters currently in the buffer, an error flag, and so on.

§4.2 stdin, stdout, and stderr Three stream are predefined and automatically available to a process: standard input, standard output, and standard error. These three standard I/O stream are referenced through the predefined file pointers stdin, stdout, and stderr. They are defined in.

§4.3 Buffering The goal of the buffering provide by the standard I/O library is to use the minimum number of read and write system calls. There are three types of buffering provided: Fully buffered. For this case actual I/O takes place when the standard I/O buffer is filled. Line buffered. In this case the standard I/O library perform I/O when a new-line character is encountered on input or output.

Unbuffered. The standard I/O library does not buffer the characters. ANSI C requires the following buffering characteristics: Standard input and standard output are fully buffered, if and only if do not refer to an interactive device. Standard error is never fully buffered.

If we don’t like these defaults for any given stream, we can change the buffering by calling either of the following two functions: #include void setbuf (FILE * fp, char * buf ); int setvbuf (FILE * fp, char * buf, int mode, size_t size );

At any time we can force a stream to be flushed: #include int fflush (FILE * fp ); If fp is NULL, this function causes all output streams to be flushed.

§4.4 Standard Stream I/O open close read write fseek Binary I/O

Opening a Stream The following three functions open a standard I/O stream: #include FILE *fopen(const char * pathname, const char * type ); FILE *freopen(const char * pathname, const char * type, FILE * fp ); FILE *fdopen(int fd, const char * type );

fopen opens a specified file freopen opens a specified file on a specified stream, closing the stream first, if it is already open. fdopen takes an existing file descriptor as its parameter, and associated a standard I/O stream with the descriptor.

Parameter type : r, rb open for reading w, wb truncate to 0 or create for writing a, ab append or create for writing r+, r+b, rb+ open for r/w w+, w+b, wb+ truncate to 0 or create for r/w a+, a+b, ab+ open or create for r/a Using the character b as part of the type allows the functions to differentiate between text file and binary file.

Closing a Stream An open stream is closed by calling fclose: #include int fclose(FILE * fp ); Any buffered output data is flushed before the file is closed. Any input data that may be buffered is discarded.

Reading and Writing a Stream Once we open a stream we can choose among three different types of unformatted I/O: Character-at-a-time I/O Line-at-a-time I/O Direct I/O

Character-at-a-time I/O Input Functions Three functions allow us to read one character at a time: #include int getc(FILE * fp ); int fgetc(FILE * fp ); int getchar(void); The function getchar is defined to be equivalent to getc(stdin).

The difference between the first two functions is that getc can be implemented as a macro when fgetc must be implemented as a function. These three functions return the next character as an unsigned char converted to an int.

These three functions return the same value EOF whether an error occurs or the end of file is reached. So we need call either ferror or feof to distinguish the two case. #include int ferror(FILE * fp ); int feof(FILE * fp ); void clearerr(FILE * fp );

After reading from a stream we can push back characters by calling ungetc : #include int ungetc (int c, FILE * fp ); A successful call to ungetc clears the end- of-file indication for the stream. Example: un-uniquely decodable code.

Output Functions We will find an output function that corresponds to each of the input functions above. #include int putc(int c, FILE * fp ); int fputc(int c, FILE * fp ); int putchar(int c );

Like the input functions, putchar(c) is equivalent to putc(c, stdout), and putc can be implemented as a macro while fputc cannot be implemented as a macro.

Line-at-a-Time I/O Input Functions #include char *fgets(char * buf, int n, FILE * fp ); char *gets(char * buf ); Both specify the address of the buffer to read the line into. gets reads from stdin while fgets reads from the specified stream. With fgets we have to specify the size of the buffer n while gets need not.

fgets reads n -1characters into the buffer and terminated with a null byte, next read will follows the line. The last read of a line will include the newline character. Another difference with gets is that it does not store the newline in the buffer, as does fgets. Even though ANSI C requires an implementation to provide gets, it should never be used.

Output Functions #include int fputs(const char * str, FILE * fp ); int puts(const char * str ); fputs will write n -1 characters to the specified stream, no mater what the characters are. puts will chop the last null byte before writes to stdout, then writes a newline character.

Standard I/O Efficiency Example 5.1 Example 5.2 Example 5.6 See 5.4.table

Binary I/O The following two functions are provided for binary I/O: #include size_t fread(void * ptr, size_t size, FILE * fp ); size_t fwrite(const void * ptr, size_t size, FILE * fp ); There are two common usage for these functions: r/w a binary array, or r/w a structure.

Positioning a Stream There are two ways to position a standard I/O stream: ftell and fseek. fgetpos and fsetpos. #include long ftell(FILE * fp ); int fseek(FILE * fp, long offset, int whence ); void rewind(FILE * fp );

int fgetpos(FILE * fp, fpos_t * pos ); int fsetpos(FILE * fp, const fpos_t * pos ); fgetpos stores the current value of the file’s position indicator in the object pointed to by pos. this value can be used in a later call to fsetpos to reposition the stream to that location.

Formatted I/O Formatted Output Functions Formatted output is handled by the three printf functions. #include int printf(const char * format, …); int fprintf(FILE * fp, const char * format, …); int sprintf(char * buf, const char * format, …); printf writes to the stdout, fprintf writes to the specified stream, and sprintf places the formatted characters in the array buf.

sprintf automatically places a null byte at the end of the array, but this null byte is not included in the return value. The following three variants of the printf family are similar to the previous three: #include int vprintf(const char * format, va_list arg ); int vfprintf(FILE * fp, const char * format, va_list arg ); int vsprintf(char * buf, const char * format, va_list arg );

Formatted Input Functions Formatted input is handled by the three scanf functions: #include int scanf(const char * format, …); int fscanf(FILE * fp, const char * format, …); int sscanf(const char * buf, const char * format, …);

Implementation Detail Under UNIX, the standard I/O library ends up calling the I/O routines that we described in Chapter 3. Each standard I/O stream has an associated file descriptor, and we can obtain the descriptor for a stream by calling fileno. #include int fileno(FILE * fp );

#define NULL 0 #define EOF (-1) #define BUFSIZ 1024 #define OPEN_MAX 20 /* max #files open at once */ typedef struct _iobuf { int cnt; /* characters left */ char *ptr; /* next character position */ char *base; /* location of buffer */ int flag; /* mode of file access */ int fd; /* file descriptor */ } FILE;

extern FILE _iob[OPEN_MAX]; #define stdin (&_iob[0]) #define stdout (&_iob[1]) #define stderr (&_iob[2]) enum _flags { _READ = 01, /* file open for reading */ _WRITE = 02, /* file open for writing */ _UNBUF = 04, /* file is unbuffered */ _EOF = 010, /* EOF has occurred on this file */ _ERR = 020 /* error occurred on this file */ };

int _fillbuf(FILE *); int _flushbuf(int, FILE *); #define feof(p) ((p)->flag & _EOF) != 0) #define ferror(p) ((p)->flag & _ERR) != 0) #define fileno(p) ((p)->fd) #define getc(p) (--(p)->cnt >= 0 ? (unsigned char) *(p)->ptr++ : _fillbuf(p)) #define putc(x,p) (--(p)->cnt >= 0 ? *(p)->ptr++ = (x) : _flushbuf((x),p)) #define getchar() getc(stdin) #define putchar(x) putc((x), stdout) Example 5.3

Temporary Files The standard I/O library provides three functions to assist in creating temporary files: #include char *tmpnam(char * ptr ); FILE tmpfile(void); char *tempnam(const char * dir, const char * prefix );

tmpnam generates a string that is a valid pathname and that is not the same as any existing file. It generates a different pathname each time it is called, up to TMP_MAX times, which is defined in. tmpfile creates a temporary binary file (type wb+) that is automatically removed when it is closed or on program termination.

tempnam is a variation of tmpnam that allow the caller to specify both the directory and a prefix for the generated pathname. tmpnam and tempnam is dangerous under Linux OS, we should use mkstemp instead. #include int mkstemp(char * template ); The mkstemp function returns the file descriptor fd of the temporary file or -1 on error.

The mkstemp function generates a unique temporary file name from template. The last six characters of template must be XXXXXX and these are replaced with a string that makes the filename unique. The file is then created with mode read/write and permissions 0666 (glibc and earlier), 0600 (glibc and later). The file is opened with the O_EXCL flag, guaranteeing that when mkstemp returns successfully we are the only user.

Alternative to Standard I/O The standard I/O library is not perfect. One inefficiency inherent in the standard I/O library is the amount of data copying that takes place. The fast I/O library gets around this by having the function that reads a line return a pointer to the line instead of copying the line into another buffer.

Summary The standard I/O library is used by most UNIX applications. Be aware of the buffering that takes place with this library, as this is the area that generates the most problems and confusion.