UNIX文件系统用户文件I/O操作详解
1. lseek()函数的使用
lseek()函数可用于在输入文件中定位到指定的起始偏移量。以下是一个示例代码,该代码将读取的数据写入标准输出:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFSZ 512
main(int argc, char *argv[])
{
char *buf;
int fd, nread;
off_t offset;
size_t iosize;
if (argc != 4) {
printf("usage: mydd filename offset size\n");
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
printf("unable to open file\n");
exit(1);
}
offset = (off_t)atol(argv[2]);
buf = (char *)malloc(argv[3]);
lseek(fd, offset, SEEK_SET);
nread = read(fd, buf, iosize);
write(STDOUT_FILENO, buf, nread);
}
使用大
超级会员免费看
订阅专栏 解锁全文
2589

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



