0

I am trying to achieve something here , I get the data from a linux system in a named pipe, the data is sporadic and does not have any determined frequency. So I have a server program in C which reads from the named pipe. But my requirement is that I have to send the data out to another program as soon I recieve the data from the client, but FREAD() function just sits on it until: a)The buffer is full and it cannot read anymore (or) b)The client closes the pipe.

The client would send every message with a delimiter of "\0", the size of the messages from the client can vary. My biggest question is how to BREAK fread after reading the message and waiting for couple of seconds and break the Fread. It just sits on the fread waiting for the data.

amountRead = fread(buffer+remaining, (size_t)1, (size_t)(BUFFER_SIZE-remaining), file); 

Basically I am trying to understand if there is any way to break the FREAD after a certain amount of time (OR) based on a delimiter?

Stedy
  • 7,359
  • 14
  • 57
  • 77

2 Answers2

1

The most straightforward approach would be to implement your own buffering solution, and use select and read, while implementing a timeout mechanism for select. This would allow you to break off the the read operation based on some time-based criteria.

As for exiting early on a delimiter character, that's not going to happen. fread is buffered and blocking, so it's going to wait until it all the data it's requesting is available.

With select, you can have a dedicated thread waiting on data to be ready, and act on it, or wait for a timeout, etc.

Please refer to the references below for working examples.

References

  1. How to implement a timeout in read function call?, Accessed 2014-06-25, <https://stackoverflow.com/questions/2917881/how-to-implement-a-timeout-in-read-function-call>
  2. What is the difference between read() and fread()?, Accessed 2014-06-25, <https://stackoverflow.com/questions/584142/what-is-the-difference-between-read-and-fread>
Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
0

Assuming the buffer size is 100 bytes and the delimiter is a comma (,):

fscanf(f, "%99[^,]", buffer);

If the delimiter or buffer size is not something you can easily hard-code, you can use snprintf to construct the format string programmatically, as in:

char fmt[10+3*sizeof(size_t)];
snprintf(fmt, sizeof fmt, "%%%zu[^%c]", sizeof buffer, delim);
fscanf(f, fmt, buffer);

Alternatively, you can loop calling getc until you get the delimiter. Depending on the implementation and the expected run length, this could be slower or faster than the fscanf method.

On POSIX 2008 conforming systems, you could alternatively use the getdelim function. This allows arbitrary input length (automatically allocated) which may be an advantage (ease of use and flexibility) or a disadvantage (bad input can exhaust all memory).

Edit: Sorry, I missed the part about needing a timeout. In that case using stdio is difficult, and you might be better off writing your own buffering system.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711