无法使用__gnu_cxx::stdio_filebuf进行流式传输
Not able to ofstream using __gnu_cxx::stdio_filebuf
这会创建文件,但它不会写入任何内容。
std::ofstream outstream;
FILE * outfile;
outfile = fopen("/usr7/cs/test_file.txt", "w");
__gnu_cxx::stdio_filebuf<char> filebuf(outfile, std::ios::out);
outstream.std::ios::rdbuf(&filebuf);
outstream << "some data";
outstream.close();
fclose(outfile);
我知道还有其他简单的解决方案可以实现输出,但我需要使用这个非标准的 filebuf 在编辑时锁定文件,以便其他进程无法打开文件。我不知道为什么这不起作用。
std::ostream
已经有一个构造函数在做正确的事情:
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <fcntl.h>
int main() {
auto file = fopen("test.txt", "w");
__gnu_cxx::stdio_filebuf<char> sourcebuf(file, std::ios::out);
std::ostream out(&sourcebuf);
out << "Writing to fd " << sourcebuf.fd() << std::endl;
}
请记住,stdio_filebuf
在FILE*
被销毁时不会关闭它,所以如果需要,请记住自己做。
相关文章:
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- Angelscript从C++传输数组
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 从服务器传输到客户端的消息不会出现
- USB传输的LibUSB C++格式不同
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 将相机数据从服务器实时流式传输到客户端
- 加密在 Windows、C++ 和 Java 中传输中的数据
- 如何从网站获取数据并将其传输到数据库?
- 使用蓝牙组件将数据从Android手机传输到串行设备时出现问题
- 在 OpenCV 中应用公式并传输图像
- 在不传输的情况下更改 Win32 中的串行端口波特率
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- Vulkan 的传输队列系列功能和显卡支持:条件检查是否准确?
- 如何使用CAPL的诊断功能获取CAN传输的数据(256字节)?
- 如何通过ROS将realsense数据传输到其他设备
- 将音频从浏览器流式传输到WebRTC本机C++应用程序
- 将FFMpeg AVFrame对象从C++应用程序流式传输到Python的最佳方法?
- 使用IMFSourceReader(Microsoft媒体基金会)进行音频流传输