版本3和4间的区别
于2007-07-18 20:55:49修订的的版本3
大小: 3669
编辑: czk
备注:
于2007-08-07 15:26:08修订的的版本4
大小: 3641
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 66: 行号 66:

[[Navigation(siblings)]]

Navigation(slides)

8.2 Low Level I/O - Read and Write

Input and output uses the read and write system calls, which are accessed from C programs through two functions called read and write. For both, the first argument is a file descriptor. The second argument is a character array in your program where the data is to go to or to come from. The third argument is the number is the number of bytes to be transferred.

   int n_read = read(int fd, char *buf, int n);
   int n_written = write(int fd, char *buf, int n);

Each call returns a count of the number of bytes transferred. On reading, the number of bytes returned may be less than the number requested. A return value of zero bytes implies end of file, and -1 indicates an error of some sort. For writing, the return value is the number of bytes written; an error has occurred if this isn't equal to the number requested.

Any number of bytes can be read or written in one call. The most common values are 1, which means one character at a time ("unbuffered"), and a number like 1024 or 4096 that corresponds to a physical block size on a peripheral device. Larger sizes will be more efficient because fewer system calls will be made.

Putting these facts together, we can write a simple program to copy its input to its output, the equivalent of the file copying program written for Chapter 1. This program will copy anything to anything, since the input and output can be redirected to any file or device.

   1    #include "syscalls.h"
   2 
   3    main()  /* copy input to output */
   4    {
   5        char buf[BUFSIZ];
   6        int n;
   7 
   8        while ((n = read(0, buf, BUFSIZ)) > 0)
   9            write(1, buf, n);
  10        return 0;
  11    }

We have collected function prototypes for the system calls into a file called syscalls.h so we can include it in the programs of this chapter. This name is not standard, however.

The parameter BUFSIZ is also defined in syscalls.h; its value is a good size for the local system. If the file size is not a multiple of BUFSIZ, some read will return a smaller number of bytes to be written by write; the next call to read after that will return zero.

It is instructive to see how read and write can be used to construct higher-level routines like getchar, putchar, etc. For example, here is a version of getchar that does unbuffered input, by reading the standard input one character at a time.

   1    #include "syscalls.h"
   2 
   3    /* getchar:  unbuffered single character input */
   4    int getchar(void)
   5    {
   6        char c;
   7 
   8        return (read(0, &c, 1) == 1) ? (unsigned char) c : EOF;
   9    }

c must be a char, because read needs a character pointer. Casting c to unsigned char in the return statement eliminates any problem of sign extension.

The second version of getchar does input in big chunks, and hands out the characters one at a time.

   1    #include "syscalls.h"
   2 
   3    /* getchar:  simple buffered version */
   4    int getchar(void)
   5    {
   6        static char buf[BUFSIZ];
   7        static char *bufp = buf;
   8        static int n = 0;
   9 
  10        if (n == 0) {  /* buffer is empty */
  11            n = read(0, buf, sizeof buf);
  12            bufp = buf;
  13        }
  14        return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
  15    }

If these versions of getchar were to be compiled with <stdio.h> included, it would be necessary to #undef the name getchar in case it is implemented as a macro.

TCPL/8.2_Low_Level_IO_-_Read_and_Write (2008-02-23 15:35:09由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.