gpt4 book ai didi

c++ - freopen 不适用于 C 标准输入

转载 作者:行者123 更新时间:2023-11-30 02:42:25 25 4
gpt4 key购买 nike

执行以下代码时:

#include <iostream>
using std::cout;

#include <cstdio>
/* stdin
stdout
FILE
freopen */

int main() {
for (FILE* f : {stdin, stdout})
if (nullptr == freopen(nullptr, (f == stdin) ? "rb" : "wb", f))
cout << f << '\n';
}

...我得到以下输出:

0x7edde180

... 这意味着 freopen(nullptr, "rb", stdin) 返回了 nullptr,失败了。

我做错了什么吗?

编译器:GNU 操作系统上的i58​​6-mingw32msvc-g++

环境: Wine (x86)

最佳答案

http://msdn.microsoft.com/en-us/library/wk2h68td.aspx

If path, mode, or stream is a null pointer, or if filename is an empty string, these functions invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return NULL.

简而言之,freopen 的 MSVC++ 实现似乎不允许 NULL 路径名。

关于c++ - freopen 不适用于 C 标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080387/

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