Answer the question
In order to leave comments, you need to log in
How to pass the contents of a file to another process through a named pipe? And output to a file?
I created a named pipe and passed a string to it using these 2 programs:
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 512
int main (void)
{
FILE * fifo;
char * buf;
if (mkfifo ("myfifo", 0640) == -1) {
fprintf (stderr, "Can't create fifo\n");
return 1;
}
fifo = fopen (FIFO_NAME, "r");
if (fifo == NULL) {
fprintf (stderr, "Cannot open fifo\n");
return 1;
}
buf = (char *) malloc (BUF_SIZE);
if (buf == NULL) {
fprintf (stderr, "malloc () error\n");
return 1;
}
fscanf (fifo, "%s", buf);
printf ("%s\n", buf);
fclose (fifo);
free (buf);
unlink (FIFO_NAME);
return 0;
}
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define FIFO_NAME "myfifo"
int main (int argc, char ** argv)
{
int fifo;
if (argc < 2) {
fprintf (stderr, "Too few arguments\n");
return 1;
}
fifo = open (FIFO_NAME, O_WRONLY);
if (fifo == -1) {
fprintf (stderr, "Cannot open fifo\n");
return 1;
}
if (write (fifo, argv[1], strlen (argv[1])) == -1) {
fprintf (stderr, "write() error\n");
return 1;
}
close (fifo);
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question