cifz
cifz

Reputation: 1078

Read line by line through a fifo

I'm an absolute beginner with Unix programming, I've to make a program that reads from the standard input some commands and then another program that execute those commands. I've to use a fifo in order to establish a communication between those two program. My problem is: how can I make possible reading line by line (one command) from a fifo where those line doesn't have a fixed size ? I was thinking to implement with signal a sort of synchronization, however I'm sure there are better ways.

I'm sorry if it's a banal question, but I'm a perfect newbie with this things.

Oh and sorry if my English is bad.

Upvotes: 3

Views: 2328

Answers (1)

Duck
Duck

Reputation: 27552

I assume you were reading with read()? Just use fgets() and let the stdlib handle the buffering. If you used open() rather than fopen() then get your FILE structure with fdopen() first. Something like:

FILE * pFile;

char mystring [100];

pFile = fopen("myfifo" , "r");

if (pFile == NULL)
{   perror ("Error opening fifo");
    exit(1);
}

while (fgets(mystring, 100, pFile) != NULL)
    puts(mystring);

fclose (pFile);

Upvotes: 3

Related Questions