【发布时间】:2014-08-15 15:32:54
【问题描述】:
我正在使用std::ifstream 从 Linux 上的命名管道中读取数据。如果文件的写入端关闭,我将无法通过流继续从管道读取。出于某种原因,我必须再次 clear()、close() 和 open() 流继续阅读。这是预期的吗?当作家close()和open()随意使用管道时,如何避免管道上的close() open()?
背景:我相信我必须做的close() open() 会导致作者有时会收到SIGPIPE,这是我想避免的。
更多细节 - 我正在使用此代码来读取流
// read single line
stream_("/tmp/delme", std::ios::in|std::ios::binary);
std::getline(stream_, output_filename_);
std::cout << "got filename: " << output_filename_ << std::endl;
#if 0
// this fixes the problem
stream_.clear();
stream_.close();
stream_.open("/tmp/delme", std::ios::in|std::ios::binary);
// now the read blocks until data is available
#endif
// read more binary data
const int hsize = 4096+4;
std::array<char, hsize> b;
stream_.read(&b[0], hsize);
std::string tmp(std::begin(b), std::begin(b)+hsize);
std::cout << "got header: " << tmp << std::endl;
/tmp/delme 是我的管道。我做了echo "foo" > /tmp/delme,我在output_filename_ 中得到了foo,但是流并没有在那里阻塞,(应该没有更多数据),它继续读取垃圾。如果我在ifdef 中启用代码,它就可以工作。为什么?
谢谢, 塞巴斯蒂安
【问题讨论】:
标签: c++ named-pipes