如果cout、cerr和clog的rdbuf已更改为重定向到文件,是否有必要重置它们
Is it necessary to reset rdbuf of cout, cerr, and clog if they have been changed to be redirected to a file?
在试图找出如何回答时https://stackoverflow.com/questions/33601384/what-is-the-file-descriptor-of-linuxs-environments-standard-logging-stream,我注意到一个链接,链接到一个相关SO帖子的答案。我用g++4.8.4尝试了上面链接答案中的代码,在程序终止前出现了分段错误。
这是程序:
#include <iostream>
#include <fstream>
int main()
{
std::ofstream of("cout.txt");
std::cout.rdbuf(of.rdbuf());
std::cout << "test. test. test." << std::endl;
return 0;
}
构建程序的命令:
g++ -Wall -std=c++11 -g socc.cc -o socc
gdb
:输出
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from socc...done.
(gdb) run
Starting program: /.........../socc (removed some text here)
Traceback (most recent call last):
File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'
Program received signal SIGSEGV, Segmentation fault.
0x000000000040073c in ?? ()
(gdb) bt
#0 0x000000000040073c in ?? ()
#1 0x0000000000000000 in ?? ()
(gdb)
我更新了程序以保留cout
的旧rdbuf
,并在程序结束前将其重置。
#include <iostream>
#include <fstream>
int main()
{
std::ofstream of("cout.txt");
auto cout_buff = std::cout.rdbuf();
std::cout.rdbuf(of.rdbuf());
std::cout << "test. test. test." << std::endl;
std::cout.rdbuf(cout_buff);
return 0;
}
有了这个更改,程序运行起来没有任何问题。
我用cerr
和clog
进行了类似的实验,结果相同。
这引出了我的问题:
如果cout
、cerr
和clog
的rdbuf
已更改为重定向到文件,是否总是需要重置它们?
如果不是,这是g++缺陷吗?
引用C++标准库-教程和参考第2版,作者:Nicolai Josuttis,
Ch。15.12.13,重定向标准流第822页
std::cout.rdbuf (file.rdbuf());
小心对象
file
是本地的,在块的末尾被销毁。这也破坏了相应的流缓冲区。这与"普通"流不同,因为文件流在构建时分配其流缓冲区对象,并在销毁时销毁它们。因此,在该示例中,cout
不能再用于写入。事实上,它甚至不能在程序终止时安全地销毁。因此,旧的缓冲区应该始终保存并在以后恢复!
所以答案似乎是肯定的,尽管我没有一句标准的名言,但上面的作者是一位相当专业的人。
相关文章:
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在设计 SDK 时,我是否应该在 C++ 头文件中完全隐藏内部类?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- 是否可以在没有真实文件的情况下创建 ifstream
- 是否可以使用 libclang python 解析 cpp 文件中没有标头的函数原型
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- 内联函数的函数本地静态对象是否在共享对象文件之间共享?
- 是否可以在命令行中将输入参数传递给可执行文件
- 在头文件上包含 cpp 文件是否有缺点?
- 如果包含映射的静态库与可执行文件和动态库链接,静态映射(变量)是否会被多次释放?
- 是否可以在文本文件中找到最长单词的长度,并在同一个文本文件中读取,只需 1 个 while 循环?
- 对同一文件使用 .read 的次数是否有限制?
- 如何检查流中文件的下一个单词是否为 alpha?
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 使用 #include 将大量重复代码放入单独的文件中是否可以和/或正常?
- 如何检查文件是否已存在于 fstream C++中
- 是否有正确的方法对生成文件中的对象文件使用模板命令?(C++)