UNIX系统编程:从底层I/O到标准I/O库
底层I/O的局限与操作
底层I/O是UNIX操作系统提供的基础输入输出接口,不具备格式化和数据转换能力。以一个程序为例,它通过以下代码实现字符串的查找和输出:
/*
* Prompt for the string number.
*/
write(1, "Which string (0 to quit)? ", 26);
n = read(0, answer, sizeof(answer));
answer[n-1] = '\0';
n = atoi(answer);
if (n == 0) {
close(fd);
exit(0);
}
if (n < 0 || n > NSTRINGS) {
write(2, "Out of range.\n", 14);
continue;
}
/*
* Find the string and read it.
*/
lseek(fd, (n-1) * STRSIZE, SEEK_SET);
read(fd, buf, STRSIZE);
/*
* Print it out.
*/
write(1, "String ", 7);
write(1, answer, strlen(answer));
write(1, " = ", 3);
write(1, buf, STRSIZE);
write(1, "\n\n", 2);
这个程序的运行示例如下:
% seeker
Whi
超级会员免费看
订阅专栏 解锁全文
1317

被折叠的 条评论
为什么被折叠?



