【问题标题】:C: reading file without system call open (only read!)C: 不打开系统调用读取文件(只读!)
【发布时间】:2018-06-27 06:14:31
【问题描述】:

我只是想将文件的数据读入内存,但我希望我的程序使用尽可能少的系统调用。这意味着我试图避免 open 或 openat。我只想使用阅读。但我不知道如何做到这一点。有人可以帮我吗?

谢谢!

【问题讨论】:

  • 不先打开就无法阅读。

标签: c system-calls


【解决方案1】:

read 需要一个打开的文件描述符,如果不调用 openopenat 就无法获得它,唯一的例外是从 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.

【讨论】:

  • 你能用标准或例如的报价来支持它吗? cppreference.com?只是为了让一个像样的答案更好。
猜你喜欢
  • 2023-02-12
  • 2014-06-01
  • 2015-01-22
  • 1970-01-01
  • 2015-01-02
  • 2014-04-19
  • 2019-09-30
  • 2012-12-08
  • 1970-01-01
相关资源
最近更新 更多