gpt4 book ai didi

c++ - 我如何使用 ifstream 从 FIFO 中读取信息?

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:59 25 4
gpt4 key购买 nike

我需要在一个循环中从 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 而不是 >>> 运算符 - 结果相同。

最佳答案

设置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
}
}

关于c++ - 我如何使用 ifstream 从 FIFO 中读取信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com