gpt4 book ai didi

c++ - 在 C++ 中调用手册第 2 节中的函数

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:37 26 4
gpt4 key购买 nike

Unix 手册的第 2 部分涵盖了系统调用(尽管它们实际上可能由系统 libc 实现 - 例如 Linux 上的 fork(2))。以下是调用 open(2)close(2) 的非常愚蠢的示例。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int
main(int argc, char *argv[]) {
int fd;

fd = open("/dev/null", O_APPEND);
close(fd);
}

自从我编写大量 C++ 代码以来已经有一段时间了。我很好奇 C++ 库现在是否有更多的 C++ 方式来访问这些函数而不必包含大量 C 头文件?上面的代码本质上是直接的 C。

如果你想移动文件描述符(确保 fd 的 0、1 和 2 是你想要的)或执行 ioctls 等,较低级别的函数仍然有用。但是枚举和一些更好的类型可能会很好对于参数。

最佳答案

I'm curious if C++ libraries now have a more C++ way to access these functions w/o having to include a lot of C header files?

他们没有,因为这些函数是由 POSIX 提供的,而不是该语言的特性。但是,提供的是 iostream抽象(C++98,第 27 章)执行“第 2 节”的大部分通用功能 open()/close()/read()/write()电话。

C++11 提供了更多的抽象,涵盖了 POSIX、Windows(等)API 提供的更多功能。

关于c++ - 在 C++ 中调用手册第 2 节中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18261261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com