Chapter Summary

I l @ ve RuBoard

Chapter Summary

Writing to and reading from files is essential for most C programs. Most C implementations offer both low-level I/O services and standard high-level I/O services for these purposes. Because the ANSI C library includes the standard I/O services but not the low-level services, the standard package is more portable.

The standard I/O package automatically creates input and output buffers to speed up data transfer. The fopen() function opens a file for standard I/O and creates a data structure designed to hold information about the file and the buffer. The fopen() function returns a pointer to that data structure, and this pointer is used by other functions to identify the file to be processed . The feof() and ferror () functions report the reason an I/O operation failed.

ANSI C provides two file-opening modes: binary and text. When a file is opened in binary mode, it can be read byte-for-byte. When a file is opened in text mode, its contents may be mapped from the system representation of text to the C representation. For UNIX systems, the two modes are identical because the C model for text files is derived from the UNIX file model.

The input functions getc() , fgets() , fscanf() , and fread() normally read a file sequentially, starting at the beginning of the file. However, the fseek() and ftell () functions let a program move to an arbitrary position in a file, enabling random access. The fgetpos () and fsetpos () extend similar capabilities to larger files. Random access works better in the binary than in the text mode.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net