Block reading from FIFO via ifstream object

I open the FIFO file as ifstream. As soon as the object is created, the thread blocks until I send something to FIFO (for me this is normal). Then I call getline()to get data from the stream.

How can I read-block the stream again until more data is written to the FIFO file?

thanks

+3
source share
3 answers

I have not tested this code, but I am wondering if FIFO just sets the EOF bit when you read all the available data. In this case, you could do this:

std::ifstream fifo;
std::string   line;
bool          done = false;

/* code to open your FIFO */

while (!done)
{
    while (std::getline(fifo, line))
    {
        /* do stuff with line */
    }
    if (fifo.eof())
    {
        fifo.clear();  // Clear the EOF bit to enable further reading
    }
    else
    {
        done = true;
    }
}

FIFO, reset . , . -. , FIFO , reset.

+3

getline, <string>, . "", , :

std::ifstream fifo;
std::string   line;

/* code to open your FIFO */

while (std::getline(fifo, line))
{
    /* do stuff with line */
}

FIFO , while false . - , .

+1

:

ifstream _FifoInStream(_FifoInFileName.c_str(), ifstream::in);

string CmdLine;

while (std::getline(_FifoInStream, CmdLine)) {
    cout << CmdLine << endl;
}

I am locked only when I initialize the _FifoInStream variable, i.e. open the stream. After sending the data to FIFO, I disappear while block. I need to read with FIFO endlessly every time you lock while reading.

0
source

Source: https://habr.com/ru/post/1702254/


All Articles