What Is Function Prototyping?

Chapter 11 - Complete I/O in C

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Chapter 11: Complete I/O In C
Overview
Many commonly used high-level languages have restrictive input and output mechanisms. As a result, programmers generate convoluted algorithms to perform sophisticated data retrieval and display. This is not the case with C, which has a very complete I/O (input/output) function library, although historically I/O was not even part of the C language itself. However, if you have used only simple I/O statements like Pascal’s readln and writeln statements, you’re in for a surprise. This chapter discusses the more than 20 different ways to perform I/O in C.
The standard C library I/O routines allow you to read and write data to files and devices. However, the C language itself does not include any predefined file structures. C treats all data as a sequence of bytes. There are three basic types of I/O functions: stream, console and port, and low-level.
All of the stream I/O functions treat a data file or data items as a stream of individual characters. By selecting the appropriate stream function, your application can process data in any size or format required, from single characters to large, complicated data structures.
Technically, when a program opens a file for I/O using the stream functions, the opened file is associated with a structure of type FILE (predefined in STDIO.H) that contains basic information about the file. Once the stream is opened, a pointer to the file structure is returned. The file pointer, sometimes called the stream pointer or the stream, is used to refer to the file for all subsequent I/O.
All stream I/O functions provide buffered, formatted, or unformatted input and output. A buffered stream provides an intermediate storage location for all information that is input from the stream and output that is being sent to the stream.
Since disk I/O is such a time-consuming operation, stream buffering streamlines the application. Instead of inputting stream data one character at a time or one structure’s worth at a time, stream I/O functions access data a block at a time. As the application needs to process the input, it merely accesses the buffer, a much less time-consuming process. When the buffer is empty, another disk block access is made.
The reverse situation holds true for stream output. Instead of all data being physically output at the time the output statement is executed, all output data is put into the buffer. When the buffer is full, the data is written to the disk.
Most high-level languages have a problem with buffered I/O that you need to take into consideration. For example, if your program has executed several output statements that do not fill the output buffer, causing it to dump to the disk, that information is lost when your program terminates.
The solution usually involves making a call to an appropriate function to flush the buffer. Unlike other high-level languages, C solves this problem with buffered I/O by automatically flushing the buffer’s contents whenever the program terminates. Of course, a well-written application should not rely on these automatic features, but should always explicitly detail every action the program is to take. One additional note: when you use stream I/O, if the application terminates abnormally, the output buffers may not be flushed, resulting in loss of data.
Similar in function are the console and port I/O routines, which can be seen as an extension of the stream routines. They allow you to read or write to a terminal (console) or an input/output port (such as a printer port). The port I/O functions simply read and write data in bytes. Console I/O functions provide several additional options. For example, you can detect whether a character has been typed at the console and whether or not the characters entered are echoed to the screen as they are read.
The last type of input and output is called low-level I/O. None of the low-level I/O functions perform any buffering and formatting; instead, they invoke the operating system’s input and output capabilities directly. These routines let you access files and peripheral devices at a more basic level than the stream functions. Files opened in this mode return a file handle. This handle is an integer value that is used to refer to the file in subsequent operations.
In general, it is very bad practice to mix stream I/O functions with low-level routines. Since stream functions are buffered and low-level functions are not, attempting to access the same file or device by two different methods leads to confusion and eventual loss of data in the buffers. Therefore, either stream or low-level functions should be used exclusively on a given file.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

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