APUE学习笔记之文件I/O(上)(3)

3 File I/O (文件I/O)

  3.1 Introduction (介绍)

  Most file I/O on a UNIX system can be performed using only five functions: open, read, write, lseek, and close. (在UNIX系统上大多数文件I/O可以执行使用open, read, write, lseek, 和 close这五个函数。)

  We describe the dup, fcntl, sysnc, fsync and ioctl functions.

  3.2 File Descriptors. (文件描述符)

  A file descriptor is a non-negative integer. (文件描述符十一个非负整数。)

  Unix System shells associate file descriptor 0 with the standard input of a process, file descriptor 1 with the standard output, and file descriptor 2 with the standard error. (Unix 系统shell把文件描述符0和标准输入关联,1和标准输出关联,2和标准错误关联。)

  The magic numbers 0,1, and 2 should be replaced in POSIX-compliant applications with the symbolic constants STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO. (POSIX 兼容应用程序把神奇的0,1,2三个数字替换为STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO三个符合常量。)

  The contants are defined in the <unistd.h> header. (常量在<unistd.h>头文件中定义)。

  3.3 open Function (open 函数)

  #include <fcntl.h>

  int open(const char *pathname, int oflag, ... /* mode_t mode */ );

                Returns: file descriptor if OK, -1 on error.

  we show the third arguments as ... , which is the ISO C way to specify that the number and types of the remaining arguments may very. (第三个参数...,是ISO C指定的方法用来表示剩下的参数的数量和类型是可变的。)

  pathname:the name of the file to open or create.

  oflag:O_RDONLY Open for reading only; (读)

      O_WRONLY Open for wrinting only; (写)

      O_RDWR Open for reading and writeing; (读写)

      One and only one of these three constants must be specifed. (有且只有一个常量必须被指定。)

        O_APPEND Append to the end of file on each write. (追加)

     O_CREATE  Create the file if it doesn't exist. This option requires a third argument to the open function, the mode, which specifies the access permission bits of the new file. (文件不存在时创建文件。需要第三个参数,mode, 指定新文件的访问权限位。)

     O_EXCL  Gernerate an error if O_CREATE is also specified and the file already exists. (如果指定O_CREATE了选项,并且文件已经存在生成一个错误。)

     O_TRUNC  If the file exists and if it is successfully opened for either write-only or read-write, truncate its length to 0. (如果该文件存在并且成功打开只写读写截断其长度为0)

     O_NOCTTY If the pathname refers to a terminal device, do not allocate the divice as the controlling terminal for this process. (如果pathname指向一个终端设备,在这个进程中不把设备作为控制终端。)

       O_NONBLOCK If the pathname refers to a FIFO, a block special file, or a character special file, this option sets the nonblocking mode for both the opening of the file and subsequent I/O. (如果指向FIFO,块特殊文件,一个字符特殊文件,这个选项为打开文件和随后的I/O设置了非块模式 。)

     O_DSYNC: Have each write wait for physical I/O to complete, but don't wait for file attributes to update. (每次写操作要等物理I/O完成,但不需等文件属性更新。)

     O_RSYNC: Have each read operation on the file descriptor wait until any pending writes for the same portion of the file are complete. (对文件的读操作要等到相同部分挂起的写操作完成。)

    O_SYNC: Have each write wait for physical I/O to complete, including I/O necessary to update file attributes modified as a result of the write. (每次写操作等物理I/O完成,包括I/O必须的文件属性更新。)

  The file descriptor returned by open is guaranteed to be the lowest-numbered unused descriptor. (open函数返回的文件描述符是未使用的最小的数字。)

  Filename and Pathname Truncation (文件名和路径名截断)

  If _POSIX_NO_TRUNC is in effect, errno is set to ENAMETOOLONG, and an error status is returned if the entire pathname exceeds PATH_MAX or any filename component of the pathname exceeds NAME_MAX. ( 如果_POSIX_NO_TRUNC生效,errno 被设置成ENAMETOOLONG,并且如果路径名超过PATH_MAX或者路径中的文件名超过NAME_MAX,会返回一个错误的状态。)

  3.4 create Function

  #include <fcntl.h>

  int create(const char *pathname, mode_t mode);

              Returns: file descriptor opened for write-only if OK, -1 on error.

  Note that this function is equivalent to open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);

  3.5 close Function

  #include <unistd.h>

  int close(int filedes);

             Returns: 0 if OK, -1 on error.
  Closing a file also releases any record locks that the process may have on the file. (关闭文件也释放进程可能对文件任何记录锁。)

  When a process terminates, all of its open files are closed automatically by the kernel. (当进程终止,它的所有打开的文件都会被kernel自动关闭。)

  3.6 lseek Function

  #include <unistd.h>

  off_t lseek(int filedes, off_t offset, int whence);

             Returns: new file offset if OK, -1 on error.

  Every open file has an associated "current file offset", normally a non-negative integer that measures the number of bytes from the beginning of the file. (每个打开的文件都有一个关联的“当前文件位置”,通常是一个非负整数来测量该文件的开头的字节数。)

  By default, this offset is initialized to 0 when a file is opened, unless the O_APPEND option is specified. (打开文件时,如果未指定O_APPEND选项,offset默认初始化为0。)

  If whence is SEEK_SET: offset is set to offset bytes from the beginning of the file. (设置成从文件开头第offset个字节的位置。)

         SEEK_CUR: offset is set to its current value plus the offset. offset can be positive ot  negative. (当前偏移量加上offset个字节,offset可以为正数或者负数。)

         SEEK_END: offset is set to the size of the file plus the offset. offset can be positive ot  negative. (文件的结尾加上offset个字节,offset可以为正数或者负数。)

  We can seek zero bytes from the current position to determine the current offset. (我们可以从当前位偏移量动0个字节来确定当前的offset。)

  offset currpos;

  currpos = lseek(fd, 0, SEEK_CUR);

  This also can determine if a file is capable of seeking. If descriptor refers to a pipe, FIFO, or socket, lseek sets errno to ESPIPE and returns -1. (这也可以确定一个文件是否可以seek, 如果文件描述符指向pipe, FIFO, 或者 socke,lseek 函数设置 errno 为 ESPIPE 并且返回 -1。)

  A file's current offset possible be a negative when certain devices. (对于某些设备offset可能会是负数。)

  We should be careful to compare the return value form lseek as being equal or not equal to -1 and not test if it's less than 0. (我们应该比较lseek的返回值是不是等于-1,而不要比较是不是小于0。)

  The file's offset can be greater than the file's current size, this is referred to as creating a hole in a file. (文件的offset可能大于文件当前的大小,这样会创建一个洞。)

  Any bytes in a file that have not been written are read back as 0. (文件中未写入的部分,读取到的结果为0。)

  A hole in a file isn't required to have storage backing it on disk. (文件中的洞无须在硬盘中存取。)

  3.7 read Function

  #include <unistd.h>

  ssize_t read(int filedes, void *buf, size_t nbytes);

              Returns: number of bytes read, 0 if end of file, -1 on error.

  The read operation starts at the file's current offset. Before a successul return, the offset is incremented by the number of bytes actually read. (读操作开始于该文件的当前偏移量, 在成功返回前,偏移量是实际读取的字节的数量递增。)

  3.8 write Function

  #include <unistd.h>

  ssize_t write(int filedes, const void *buf, size_t nbytes);

              Returns: number of bytes written if OK, -1 on error.

 

  The return value is usually equal to the nbytes argument; otherwise, an error has occurred. (返回值通常等于参数nbytes,否则报错。)

  After a successful write, the file's offset is incremented by the number of bytes actually written. (写操作成功后,文件的偏移量根据实际写入的字节递增。)

  (完)

  

  

  

posted @ 2013-09-19 12:17  yanjf  阅读(203)  评论(0编辑  收藏  举报