Flylib.com

Books Software

 
 
 

Pointer Variables

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

Low-level Input and Output in C
Table 11-1 lists the most commonly used low-level input and output functions used by an application.
Table 11-1: Commonly Used Low-level Input and Output Functions
Function
Definition
close()
Closes a disk file
lseek ()
Seeks to the specified byte in a file
open ()
Opens a disk file
read()
Reads a buffer of data
unlink()
Removes a file from the directory
write()
Writes a buffer of data
Low-level input and output calls do not buffer or format data. Files opened by low-level calls are referenced by a file handle (an integer value used by the operating system to refer to the file). You use the open() function to open files. You can use the sopen() macro to open a file with file-sharing attributes.
Low-level functions are different from their stream counterparts because they do not require the inclusion of the STDIO.H header file. However, some common constants that are predefined in STDIO.H, such as EOF and NULL , may be useful. Declarations for the low-level functions are given in the IO.H header file.
This second disk-file I/O system was originally created under the UNIX operating system. Because the ANSI C standard committee has elected not to standardize this low-level, UNIX-like, unbuffered I/O system, it cannot be recommended for future use. Instead, the standardized buffered I/O system described throughout this chapter is recommended for all new projects.

Books24x7.com, Inc 2000 –  

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

Character Input and Output
There are certain character input and output functions defined in the ANSI C standard that are supplied with all C compilers. These functions provide standard input and output and are considered to be high-level routines (as opposed to low-level routines, which access the machine hardware more directly). I/O in C is implemented through vendor-supplied functions rather than keywords defined as part of the language.
Using getc(), putc(), fgetc (), and fputc ()
The most basic of all I/O functions are those that input and output one character. The getc() function inputs one character from a specified file stream, like this:
int ic;
ic = getc(stdin);
The input character is passed back in the name of the function getc() and then assigns the returned value to ic . By the way, if you are wondering why ic isnt of type char , it is because the function getc() has been prototyped to return an int type. This is necessary because of the possible system-dependent size of the end-of-file marker, which might not fit in a single char byte size.
Function getc() converts the integer into an unsigned character. This use of an unsigned character preserved as an integer guarantees that the ASCII values above 127 are not represented as negative values. Therefore, negative values can be used to represent unusual situations like errors and the end of the input file. For example, the end-of-file has traditionally been represented by - 1, although the ANSI C standard states only that the constant EOF represents some negative value.
Because an integer value is returned by getc(), the data item that inputs the value from getc() must also be defined as an integer . While it may seem odd to be using an integer in a character function, the C language actually makes very little distinction between characters and integers. If a character is provided when an integer is needed, the character will be converted to an integer .
The complement to the getc() function is putc(). The putc() function outputs one character to the file stream represented by the specified file pointer. To send the same character that was just input to the standard output, use the following statement:
putc(ic, stdout );
The getc() function is normally buffered, which means that when a request for a character is made by the application, control is not returned to the program until a carriage return is entered into the standard input file stream. All the characters entered before the carriage return are held in a buffer and delivered to the program one at a time. The application invokes the getc() function repeatedly until the buffer has been exhausted. After the carriage return has been sent to the program by getc(), the next request for a character results in more characters accumulating in the buffer until a carriage return is again entered. This means that you cannot use the getc() function for one-key input techniques that do not require pressing the carriage return.
One final note: getc() and putc() are actually implemented as macros rather than as true functions. The functions fgetc() and fputc() are identical to their macro getc() and putc() counterparts.
Using getchar (), putchar (), fgetchar(), and fputchar()
The two macros getchar() and putchar() are actually specific implementations of the getc() and putc() macros, respectively. They are always associated with standard input ( stdin ) and standard output ( stdout ). The only way to use them on other file streams is to redirect either standard input or standard output from within the program.
The same two coded examples used earlier could be rewritten by using these two functions:
int ic;
ic = getchar();
and
putchar(ic);
Like getc() and putc(), getchar() and putchar() are implemented as macros. The function putchar() has been written to return an EOF value whenever an error condition occurs. The following code can be used to check for an output error condition. Because of the check for EOF on output, it tends to be a bit confusing, although it is technically correct.
if(putchar(ic) == EOF)
 printf(An error has occurred writing to stdout);
Both fgetchar() and fputchar() are the function equivalents of their macro getchar() and putchar() counterparts.
Using getch() and putch()
Both getch() and putch() are true functions, but they do not fall under the ANSI C standard because they are low-level functions that interface closely with the hardware. For PCs, these functions do not use buffering, which means that they immediately input a character typed at the keyboard. They can be redirected, however, so they are not associated exclusively with the keyboard.
You can use the functions getch() and putch() exactly like getchar() and putchar(). Usually, a program running on a PC will use getch() to trap keystrokes ignored by getchar() for example, pgup, pgdn, home, and end. The function getchar() sees a character entered from the keyboard as soon as the key is pressed; a carriage return is not needed to send the character to the program. This ability allows the function getch() to provide a one-key technique that is not available with getc() or getchar().
On a PC, the function getch() operates very differently from getc() and getchar(). This is partly due to the fact that the PC can easily determine when an individual key on the keyboard has been pressed. Other systems, such as the DEC and VAX C, do not allow the hardware to trap individual keystrokes. These systems typically echo the input character and require the pressing of a carriage return, with the carriage return character not seen by the program unless no other characters have been entered. Under such circumstances, the carriage return returns a null character or a decimal zero. Additionally, the function keys are not available, and if they are pressed, they produce unreliable results.

Books24x7.com, Inc 2000 –