title: How to append into a named pipe without changing the value stored previously (linux)(named pipes)
body: <p>I am creating 10 children to one parent. I want all the children to write into a pipe appending the pipe, so I can then read the collective data later in the parent.
  So first child writes into the pipe &quot;9-6&quot; then the second child writes &quot;9-6&quot; making the contents inside the pipe &quot;9-6 9-6&quot;
  but what I have discovered here is that Every child when open the pipes for writing inside it. It truncates the pipe.
  Is there a way I can just keep on adding content into the pipe and eventually in the end just read it.</p>
  <pre><code>#include  &lt;fcntl.h&gt;                              //
  #include  &lt;stdio.h&gt;                              //
  #include  &lt;stdlib.h&gt;                             //
  #include  &lt;string.h&gt;                             //
  #include  &lt;sys/types.h&gt;                          //
  #include  &lt;sys/wait.h&gt;                           //
  #include  &lt;sys/stat.h&gt;                           //
  #include  &lt;termios.h&gt;                            //
  #include  &lt;unistd.h&gt; 
  #include &lt;iostream&gt; 
  
  using namespace std;
  
  
  int main()
  {
      pid_t pids[10]; //10 children
      int i;
      int n = 10;
      
  
      int f1 = mkfifo(&quot;p&quot;, 0666);   //making named pipe                                   
      
      if (f1 &lt; 0)
          std::cerr &lt;&lt; &quot;Pipe not created&quot;;
  
      char str[256] = &quot;9-6&quot;;   //character array that every child writes in the pipe
      char read_char[256] = &quot;&quot;;                                            
      /* Start children. */
      for (i = 0; i &lt; 10; ++i) {
          if ((pids[i] = fork()) &lt; 0) {
              perror(&quot;fork&quot;);
              abort();
          }
          else if (pids[i] == 0) {\
  
              int fifo_write = open(&quot;p&quot;, O_WRONLY); //open the pipe for writing
              if (fifo_write &lt; 0)
              {
                  std::cerr &lt;&lt; &quot;Pipe could not be created&quot;;
                  return 0;
              }
              else
              {
                 
                  write(fifo_write, str, sizeof(str)); //write char str[] and close the pipe
                  close(fifo_write);
                  exit(0); 
              }
              
              
          }
  
      }
  
      
      
  
      /* Wait for children to exit. */
     
      int count(0); 
      int fifo_read = open(&quot;p&quot;, O_RDONLY);
      char str1[256] = &quot;&quot;; 
      if (fifo_read &lt; 0)
      {
          std::cerr &lt;&lt; &quot;Pipe could not be created&quot;;
      }
      else
      {
              
          read(fifo_read, str1, sizeof(str));
          cout &lt;&lt; str1 &lt;&lt; endl;
          count++; 
          close(fifo_read);
      }
      cout &lt;&lt; &quot;the count is &quot; &lt;&lt; count &lt;&lt; endl; 
      unlink(&quot;p&quot;);
  }
  </code></pre>
  <p>I have also tried first reading the pipe and appending what is read from the pipe with &quot;9-6&quot; and then writing in the pipe again.
  The implementation of that looks like this</p>
  <pre><code>#include  &lt;fcntl.h&gt;                              //
  #include  &lt;stdio.h&gt;                              //
  #include  &lt;stdlib.h&gt;                             //
  #include  &lt;string.h&gt;                             //
  #include  &lt;sys/types.h&gt;                          //
  #include  &lt;sys/wait.h&gt;                           //
  #include  &lt;sys/stat.h&gt;                           //
  #include  &lt;termios.h&gt;                            //
  #include  &lt;unistd.h&gt; 
  #include &lt;iostream&gt; 
  
  using namespace std;
  
  
  
  
  
  int main()
  {
      pid_t pids[10];
      int i;
      int n = 10;
      
  
      int f1 = mkfifo(&quot;p&quot;, 0666);                                         //Making named pipe
      
      if (f1 &lt; 0)
          std::cerr &lt;&lt; &quot;Pipe not created&quot;;
  
      char str[256] = &quot;9-6&quot;;   
      char read_char[256] = &quot;&quot;;                                            
      /* Start children. */
      for (i = 0; i &lt; 10; ++i) {
          if ((pids[i] = fork()) &lt; 0) {
              perror(&quot;fork&quot;);
              abort();
          }
          else if (pids[i] == 0) {
  //here I open the pipe for read so that I can read what inside and concatenate it with 9-6 //so every time it is concatenated with 9-6 and written back in the pipe
              int fifo_read = open(&quot;p&quot;, O_RDONLY); 
              if (fifo_read &lt; 0)
              {
                  std::cerr &lt;&lt; &quot;Pipe could not be created&quot;;
                  return 0;
              }
              else
              {
                  read(fifo_read, read_char, sizeof(read_char)); 
                  strcat(str, read_char); 
                  close(fifo_read); 
  
              }
              int fifo_write = open(&quot;p&quot;, O_WRONLY);
              if (fifo_write &lt; 0)
              {
                  std::cerr &lt;&lt; &quot;Pipe could not be created&quot;;
                  return 0;
              }
              else
              {
                 
                  write(fifo_write, str, sizeof(str));
                  close(fifo_write);
                  exit(0); 
              }
              
              
          }
  
      }
  
      
      
  
      /* Wait for children to exit. */
     
      int count(0); 
      int fifo_read = open(&quot;p&quot;, O_RDONLY);
      char str1[256] = &quot;&quot;; 
      if (fifo_read &lt; 0)
      {
          std::cerr &lt;&lt; &quot;Pipe could not be created&quot;;
      }
      else
      {
              
          read(fifo_read, str1, sizeof(str));
          cout &lt;&lt; str1 &lt;&lt; endl;
          count++; 
          close(fifo_read);
      }
      cout &lt;&lt; &quot;the count is &quot; &lt;&lt; count &lt;&lt; endl; 
      unlink(&quot;p&quot;);
  }
  </code></pre>
  <p>But when I do this mkfifo fails and &quot;pipe not created&quot; is printed</p>