我如何使用 ifstream 从 FIFO 中读取信息?
How may I use the ifstream to read an info from a FIFO?
我需要在一个循环中从 FIFO 中读取信息(字符串),而另一个应用程序将写入其中。我如何使用 ifstream 来执行此操作?
我试过像这样的简单算法:
ifstream fifo("/path/to/file");
while(true)
{
// Check, if we need to break the cycle etc...
if(!fifo.eof())
{
string s;
fifo >> s;
// Do something...
}
}
但它只读取第一行。我试图添加 seekg(0)
的调用,但这没有得到任何结果。我尝试使用 getline
而不是 >>
运算符 - 结果相同。
试试这个
ifstream fifo;
fifo.open("/path/to/file");
char output[100];
if (fifo.is_open()) {
while (!fifo.eof()) {
fifo >> output;
//do whatever you want with the output
}
}
else cout << "Cant open the file"<<endl;
设置 eof 标志后,它将一直保留到您清除它为止。以下可能有效:
string s;
while (true) {
fifo >> s;
if (fifo.eof()) {
sleep(1); // wait for another app to write something
fifo.clear();
}
else {
// do what you want with the string s
}
}
我需要在一个循环中从 FIFO 中读取信息(字符串),而另一个应用程序将写入其中。我如何使用 ifstream 来执行此操作?
我试过像这样的简单算法:
ifstream fifo("/path/to/file");
while(true)
{
// Check, if we need to break the cycle etc...
if(!fifo.eof())
{
string s;
fifo >> s;
// Do something...
}
}
但它只读取第一行。我试图添加 seekg(0)
的调用,但这没有得到任何结果。我尝试使用 getline
而不是 >>
运算符 - 结果相同。
试试这个
ifstream fifo;
fifo.open("/path/to/file");
char output[100];
if (fifo.is_open()) {
while (!fifo.eof()) {
fifo >> output;
//do whatever you want with the output
}
}
else cout << "Cant open the file"<<endl;
设置 eof 标志后,它将一直保留到您清除它为止。以下可能有效:
string s;
while (true) {
fifo >> s;
if (fifo.eof()) {
sleep(1); // wait for another app to write something
fifo.clear();
}
else {
// do what you want with the string s
}
}