我想要做什么:在C++的FIFO(阻塞读)工作
1,创建并开放供过程1
2.Open in.fifo在过程2
阅读写作in.fifo3.Write从CIN到in.fifo由过程1线
4.Read和cout线通过过程2
5.当输入 “退出” CIN(过程2)时,它关闭文件in.fifo,删除它并退出
6.process2退出,因为in.fifo没有作家
在我的程序进程2不退出。在C它读的作品,写的时候O_NONBLOCK是清楚的,但我想这样做在C++
write.cpp:
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main(){
std::ofstream fifo;
fifo.open("/home/fedor/projects/fifo2/in",ios::out);
if(! fifo.is_open()){
std::cout << " error : cannot open file " << std :: endl;
return 1;
}
std::cout << " file open " << std :: endl;
std::string line;
while (line.compare("exit") != 0){
std::getline(cin, line);
fifo << line << endl;
/* do stuff with line */
}
fifo.close();
remove("/home/fedor/projects/fifo2/in");
return 0;
}
read.cpp:
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main(){
std::ifstream fifo;
fifo.open("/home/fedor/projects/fifo2/in",ifstream::in);
if(! fifo.is_open()){
std::cout << " error : cannot open file " << std :: endl;
return 1;
}
std::cout << " file open " << std :: endl;
std::string line;
bool done = false;
while (!done)
{
while (std::getline(fifo, line))
{
cout << line << endl;
/* do stuff with line */
}
if (fifo.eof())
{
fifo.clear(); // Clear the EOF bit to enable further reading
}
else
{
done = true;
}
}
return 0;
}
我可以” t找到我在哪里可以读取有关阻止读取像流http://linux.die.net/man/3/read关于阻止读取
如果process2关闭,如果输入“退出”,就像process1是一个生命锁? (读取时是否阻塞,或只是尝试读取)
感谢您的回答! 我在c上有程序,就像我想要的那样工作。我如何在C++中使用open()之类的函数? – fedden 2014-08-28 11:50:05
@fedden就像你在C使用他们一样。 – 2014-08-28 11:54:39
谢谢,所有的工作。在C++中需要包含一些不同的库:)) – fedden 2014-08-28 12:25:48