Sample Header Ad - 728x90

How pass the file's content through Named Pipe and then copy it into another one?

1 vote
1 answer
1809 views
I wrote 2 programs that normally 1.create the named pipe(server) and 2. pass the string from the shell to the server part. I understand how to hand over a string from the terminal to the server part of the named pipe.But, i don't have any idea how to pass files as arguments into the 2 program so that the file's content should be read and therefore passed out to ( likely the server part of the named pipe) another file. The idea is to copy the first file's content to the second file. Unfortunately, i don't have any idea how to implement this. #include #include #include #include #include #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 #include #include #include #include #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, strlen (argv)) == -1) { fprintf (stderr, "write() error\n"); return 1; } close (fifo); return 0; }
Asked by Kirill Shvedov (11 rep)
Nov 10, 2020, 03:58 AM
Last activity: Feb 10, 2021, 02:43 AM