【发布时间】:2018-06-27 06:14:31
【问题描述】:
我只是想将文件的数据读入内存,但我希望我的程序使用尽可能少的系统调用。这意味着我试图避免 open 或 openat。我只想使用阅读。但我不知道如何做到这一点。有人可以帮我吗?
谢谢!
【问题讨论】:
-
不先打开就无法阅读。
标签: c system-calls
我只是想将文件的数据读入内存,但我希望我的程序使用尽可能少的系统调用。这意味着我试图避免 open 或 openat。我只想使用阅读。但我不知道如何做到这一点。有人可以帮我吗?
谢谢!
【问题讨论】:
标签: c system-calls
read 需要一个打开的文件描述符,如果不调用 open 或 openat 就无法获得它,唯一的例外是从 stdin (fd 0) 读取。
更新添加:
感谢@Yunnosch 的建议。这个怎么样:
http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html
名字
pread, read - read from a file
概要
#include <unistd.h>
[XSI] [Option Start] ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset); [Option End]
ssize_t read(int fildes, void *buf, size_t nbyte);
描述
The read() function shall attempt to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf.
【讨论】: