2

Recently I have tasked with customizing one of the applications that our groups uses quite regularly. The application is written in C, something that I don't have a whole lot of experience in. I have found some functional examples of what I am trying to do but as of yet have been unable to integrate it fully into my test bed.

The requirement is quite simple: open a named pipe and push some data out so that another group can read the pipe and do something with it. This has to be done such that it doesn't interrupt the server from doing its thing.

My application has an array called history which contains all the necessary values that I am after. I have so far had mixed results with my attempts at accomplishing this. Please see below snippet of code:

DCmass_add_history(history, history_num);

int fd;
char * myfifo = "/tmp/fooPipe";

/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);

/* open FIFO (named pipe) */
fd = open(myfifo, O_WRONLY);
  for (i = 0; i < history_num; i++) {

  /* Write our data to our FIFO (named pipe) */
  fprintf(fd, "%d %d %f %f", history[i].id, history[i].clock, history[i].value, history[i].ns);

}
close(fd);

I had some success previously when using the write instead of fprintf but couldn't get any variables into my pipe.

From a previous stack overflow example, Stack Overflow example I was able to get a basic reader to test my pipe, and the basic code from above to open my pipe.

Community
  • 1
  • 1
chuy08
  • 139
  • 2
  • 7

4 Answers4

3

The warning you are receiving on the fprintf() line should be a hint.

If you want to use fprintf(), you have to open a FILE * for it using fopen(), not open():

FILE *fd = fopen(myfifo, "w");

and close it using fclose().

Kijewski
  • 25,517
  • 12
  • 101
  • 143
0

You need to maintain tight control over you buffering. I suppose setlinebuf() would help but I always like to use snprintf() to format variables into a buffer and write() that totally under my control. Entire lines at once.

What may be happening to you with printf() is larger buffers, once enough lines accumulate without setlinebuf(), only part of the line will format. By explicitly using write() with buffers small enough to assure atomic writes (check your local OS for the limits) you can be assured that no line is split.

Readers need to be very careful when reading fifos to notice such split lines to continue reading up to the \n, but everyone is properly careful here and such may be your problem

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Gilbert
  • 3,740
  • 17
  • 19
0

You can write like this, if you want to use FifoFile

char * mesg;
// allocating for mesg; and then 

sprintf(msg, "%d %d %f %f", history[i].id, history[i].clock, history[i].value, history[i].ns);

write(fd, msg, sizeof(msg));

I hope, it's helpful for you!

Howli
  • 12,291
  • 19
  • 47
  • 72
Ramazan
  • 37
  • 1
0

this won't give you want

int fd;

for file missions

FILE *fd = fopen(fileName , option);

fileName like temp.txt , database.txt

option - r (readable)

-w (writable)

-r+ and w+ (write and read with add on text from the last line)

eemrah
  • 1,603
  • 3
  • 19
  • 37