每天看点英文文档:apue第三版(代码片段)

看,未来 看,未来     2023-01-02     735

关键词:

1.5 Input and Output(输入输出流)

File Descriptors

File descriptors are normally small non-negative integers that the kernel uses to identify the files accessed by a process. Whenever it opens an existing file or creates a new file, the kernel returns a file descriptor that we use when we want to read or write the file.

文件描述符通常是小的非负整数,内核使用它们来确认程序使用的文件。当我们想要读写文件的时候,不论这个文件是否已存在,内核都会返回一个文件描述符。


Standard Input, Standard Output, and Standard Error

By convention(照常), all shells open three descriptors whenever a new program is run:
standard input, standard output, and standard error. If nothing special is done, as in the simple command ls then all three are connected to the terminal. Most shells provide a way to redirect any or all of these three descriptors to any file. For example, ls > file.list executes the ls command with its standard output redirected to the file named file.list.

按照惯例,所有的shell都会打开三个文件描述符:标准输入、标准输出、标准错误。如果没有什么特殊的情况,执行一个简单的命令:ls,你就会看到这三个文件描述符已经连接在终端上了。大部分shell允许用户重定义这三个文件描述符到任何一个文件上,这好像叫:重定向。

举个例子:ls > file.list
这个操作会将ls的输出结果重定向到文件 file.list 中。


Example

If we’re willing to read from the standard input and write to the standard output, then the program in Figure 1.4 copies any regular file on a UNIX system.

如果我们想读取标准输入并写入标准输出,那么不妨看一下以下示例代码:

#include "apue.h"
#define BUFFSIZE 4096

int main(void)
	int n;
	char buf[BUFFSIZE];
	
	while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
		if (write(STDOUT_FILENO, buf, n) != n)
			err_sys("write error");
		if (n < 0)
			err_sys("read error");
	exit(0);


Standard I/O

The standard I/O functions provide a buffered interface to the unbuffered I/O functions. Using standard I/O relieves us from having to choose optimal buffer sizes, such as the BUFFSIZE constant in Figure 1.4. The standard I/O functions also simplify dealing with lines of input (a common occurrence in UNIX applications). The fgets function, for example, reads an entire line. The read function, in contrast, reads a specified number of bytes. As we shall see in Section 5.4, the standard I/O library provides functions that let us control the style of buffering used by the library.

The most common standard I/O function is printf. In programs that call printf, we’ll always include <stdio.h>—normally by including apue.h—as this header contains the function prototypes for all the standard I/O functions.

标准I/O函数为无缓冲I/O函数提供了一个缓冲接口(感觉好别扭啊),使用标准I/O使我们不必选择最佳缓冲区大小,如XXX的BUFFSIZE常量。此外,使用标准I/O使我们不必选择最佳缓冲区大小,如图1.4中的BUFFSIZE常量。标准I/O函数还简化了对输入行的处理,例如,使用fgets函数读取整行。
反之,read函数读取指定数量的字节。正如我们将在第5.4节中看到的,标准I/O库提供了一些函数,让我们可以控制库使用的缓冲样式。

最常见的标准I/O函数是printf。在调用printf的程序中,我们将始终包括<stdio.h>——包含于apue.h,因为此标头包含所有标准I/O函数的函数原型。


Example

The program in Figure 1.5, which we’ll examine in more detail in Section 5.8, is like the previous program that called read and write. This program copies standard input to standard output and can copy any regular file.

#include "apue.h"
int
main(void)

int c;
while ((c = getc(stdin)) != EOF)
if (putc(c, stdout) == EOF)
err_sys("output error");
if (ferror(stdin))
err_sys("input error");
exit(0);

Figure 1.5 Copy standard input to standard output, using standard I/O
The function getc reads one character at a time, and this character is written by putc.
After the last byte of input has been read, getc returns the constant EOF (defined in <stdio.h>). The standard I/O constants stdin and stdout are also defined in the <stdio.h> header and refer to the standard input and standard output.


今天事儿多,先到这里。

每天看点英文文档:apue第三版(代码片段)

1.6ProgramsandProcesses(程序与进程)ProgramAprogramisanexecutablefileresidingondiskinadirectory.Aprogramisreadintomemoryandisexecutedbythekernelasaresultofoneofthesevenexecfunctions.We’llcover 查看详情

每天看点英文文档:apue第三版(代码片段)

1.5InputandOutput(输入输出流)FileDescriptorsFiledescriptorsarenormallysmallnon-negativeintegersthatthekernelusestoidentifythefilesaccessedbyaprocess.Wheneveritopensanexistingfileorcreatesanew 查看详情

每天看点英文文档:apue第三版(代码片段)

1.5InputandOutput(输入输出流)FileDescriptorsFiledescriptorsarenormallysmallnon-negativeintegersthatthekernelusestoidentifythefilesaccessedbyaprocess.Wheneveritopensanexistingfileorcreatesanew 查看详情

每天看点英文文档:apue第三版(代码片段)

1.5InputandOutput(输入输出流)FileDescriptorsFiledescriptorsarenormallysmallnon-negativeintegersthatthekernelusestoidentifythefilesaccessedbyaprocess.Wheneveritopensanexistingfileorcreatesanew 查看详情

每天看点英文文档:apue第三版(代码片段)

1.4FilesandDirectories(文件和目录)FileSystem(文件系统)TheUNIXfilesystemisahierarchicalarrangement(层次排列)ofdirectoriesandfiles.Everythingstartsinthedirectorycalledroot,w 查看详情

每天看点英文文档:apue第三版(代码片段)

1.4FilesandDirectories(文件和目录)FileSystem(文件系统)TheUNIXfilesystemisahierarchicalarrangement(层次排列)ofdirectoriesandfiles.Everythingstartsinthedirectorycalledroot,w 查看详情

每天看点英文文档:apue第三版(代码片段)

1.4FilesandDirectories(文件和目录)FileSystem(文件系统)TheUNIXfilesystemisahierarchicalarrangement(层次排列)ofdirectoriesandfiles.Everythingstartsinthedirectorycalledroot,w 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情

趣味英译文:apue第三版(代码片段)

1.9SignalsSignalsareatechniqueusedtonotifyaprocessthatsomeconditionhasoccurred.Forexample,ifaprocessdividesbyzero,thesignalwhosenameisSIGFPE(floating-pointexception)issenttotheprocess.Theprocesshasthr 查看详情

趣味英译文:apue第三版(代码片段)

1.9SignalsSignalsareatechniqueusedtonotifyaprocessthatsomeconditionhasoccurred.Forexample,ifaprocessdividesbyzero,thesignalwhosenameisSIGFPE(floating-pointexception)issenttotheprocess.Theprocesshasthr 查看详情

趣味英译文:apue第三版(代码片段)

1.9SignalsSignalsareatechniqueusedtonotifyaprocessthatsomeconditionhasoccurred.Forexample,ifaprocessdividesbyzero,thesignalwhosenameisSIGFPE(floating-pointexception)issenttotheprocess.Theprocesshasthr 查看详情

趣味英译文:apue第三版(代码片段)

1.7ErrorHandling(错误处理)WhenanerroroccursinoneoftheUNIXSystemfunctions,anegativevalueisoftenreturned,andtheintegererrnoisusuallysettoavaluethattellswhy.Forexample,theopenfunctionreturnseit 查看详情

趣味英译文:apue第三版(代码片段)

1.7ErrorHandling(错误处理)WhenanerroroccursinoneoftheUNIXSystemfunctions,anegativevalueisoftenreturned,andtheintegererrnoisusuallysettoavaluethattellswhy.Forexample,theopenfunctionreturnseit 查看详情

趣味英译文:apue第三版(代码片段)

1.7ErrorHandling(错误处理)WhenanerroroccursinoneoftheUNIXSystemfunctions,anegativevalueisoftenreturned,andtheintegererrnoisusuallysettoavaluethattellswhy.Forexample,theopenfunctionreturnseit 查看详情

apue第三版习题10.5(代码片段)

这是书本上的答案:See‘‘ImplementingSoftwareTimers’’byDonLibes(CUsersJournal,vol.8,no.11,Nov.1990)foranexample.Acopyofthispaperisavailableonlineathttp://www.kohala.com/start/libes.timers.txt.  我参考上面提到 查看详情