添加到喬恩 - 漢森的回答,這裏有一個簡單的例子演示如何使用file_descriptor_source
與管道。
如何打造:
g++ -m32 -DBOOST_IOSTREAMS_NO_LIB -isystem ${BOOST_PATH}/include \
${BOOST_SRC_PATH}/libs/iostreams/src/file_descriptor.cpp blah.cc -o blah
代碼:
#include <fcntl.h>
#include <stdio.h>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
int main(int argc, char* argv[]) {
// if you just do 'using namespace...', there's a
// namespace collision with the global 'write'
// function used in the child
namespace io = boost::iostreams;
int pipefd[] = {0,0};
pipe(pipefd, 0); // If you use O_NONBLOCK, you'll have to
// add some extra checks to the loop so
// it will wait until the child is finished.
if(0 == fork()) {
// child
close(pipefd[0]); // read handle
dup2(pipefd[1], FILENO_STDOUT);
printf("This\nis\na\ntest\nto\nmake sure that\nit\nis\working as expected.\n");
return 0; // ya ya, shoot me ;p
}
// parent
close(pipefd[1]); // write handle
char *buff = new char[1024];
memset(buff, 0, 1024);
io::stream<io::file_descriptor_source> fds(
io::file_descriptor_source(pipefd[0], io::never_close_handle));
// this should work with std::getline as well
while( fds.getline(buff, 1024)
&& fds.gcount() > 0 // this condition is not enough if you use
// O_NONBLOCK; it should only bail if this
// is false AND the child has exited
) {
printf("%s,", buff);
}
printf("\n");
}
文件描述符是特定操作系統的一項功能。既然如此,便攜式語言如C++通常不會指定處理它們的函數。您可能會感到驚訝的是,C標準沒有指定任何文件描述符函數。 – 2009-12-24 11:08:33
一切都是操作系統的一個功能,包括文件和進程。我已經接受了這個特殊的功能被忽略了C++標準,我願意使用boost或其他任何能夠使我無法使用fread重寫整個類的東西。 – graw 2009-12-24 11:21:51
是的,很有趣的是,面嚮對象語言與程序語言有何不同。不同的是,一旦你理解OO,與程序相比,更容易得到正確的結果。 – 2009-12-24 17:35:26