title: Reading a line and removing Spaces from matrix in a file
body: <p>OK so I manage to write to the file <code>char</code> by <code>char</code> but it writes twice the numbers and it it still write it with spaces. Any advice and solution?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;fcntl.h&gt;
  
  extern int errno;
  #define MAX_LEN 18
  
  int main(int argc, char *argv[]) {
      int fd[2], des, bytes, target;
      char buffer[161];
      int fdr, fdw;   // file descriptors
      char c;
  
      fdr = open(argv[1],  O_RDONLY);   // open files
      fdw = open("gg.txt", O_WRONLY | O_CREAT);
      if (fdr &lt; 0 || fdw &lt; 0) {
          perror("failed to open input or output files");
          exit(EXIT_FAILURE);
      }
  
      while (read(fdr, &amp;c, 1)) { // read/write a single char from/to the files
          if (c != ' ' &amp;&amp; c != EOF) {
              if (write(fdw, &amp;c, 1) != 1) {
                  perror("write() failed");
                  exit(EXIT_FAILURE);
              }
          }    // echo char to stdout
      }
  
      close(fdr);   // close the files
      close(fdw);
      exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>EDIT SECTION</p>
  
  <p>hey again,</p>
  
  <p>i managed to read the file and write it to new one without spaces but i'm trying to insert the values to matrix but im getting an error trying to open the new file.</p>
  
  <p>i have changed the permissions </p>
  
  <pre><code>fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
  </code></pre>
  
  <p>the function </p>
  
  <pre><code>void removeSpaces(int matrix[SIZE][SIZE],int fdr, int fdw) {
  
      char c;
      char matBuffer={0};
      while (read(fdr, &amp;c, 1))          // read/write a single char
      {                                  // from/to the files
          if (c != ' ') {
              if (write(fdw, &amp;c, 1) != 1) {
                  perror("write() failed");
                  exit(EXIT_FAILURE);
              }
          }
      }
      int i;
      int j;
      int k=0;
      while(read(fdw, &amp;matBuffer, 1))
      {
          for(i=0;i&lt;SIZE;i++)
          {
              for(j=0;j&lt;SIZE;j++)
                      {
                          matrix[i][j]=matBuffer-'0';
                          k++;
                      }
              k=0;
          }
  
      }
  
  }
  </code></pre>
  
  <p>The whole program</p>
  
  <pre><code>// C program to illustrate
  // open system call
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;fcntl.h&gt;
  #define MAX_LEN 18
  #define SIZE 9
  void removeSpaces(int matrix[SIZE][SIZE],int fdr, int fdw) {
  
      char c;
      char matBuffer={0};
      while (read(fdr, &amp;c, 1))          // read/write a single char
      {                                  // from/to the files
          if (c != ' ') {
              if (write(fdw, &amp;c, 1) != 1) {
                  perror("write() failed");
                  exit(EXIT_FAILURE);
              }
          }
      }
      int i;
      int j;
      int k=0;
      while(read(fdw, &amp;matBuffer, 1))
      {
          for(i=0;i&lt;SIZE;i++)
          {
              for(j=0;j&lt;SIZE;j++)
                      {
                          matrix[i][j]=matBuffer-'0';
                          k++;
                      }
              k=0;
          }
  
      }
  
  }
  int is_safe(int matrix[9][9],int n, int r, int c)
  {
      int i,j;
      //checking in row
      for(i=0;i&lt;9;i++)
      {
          //there is a cell with same value
          if(matrix[r][i] == n)
              return 0;
      }
      //checking column
      for(i=0;i&lt;SIZE;i++)
      {
          //there is a cell with the value equal to i
          if(matrix[i][c] == n)
              return 0;
      }
      //checking sub matrix
      int row_start = (r/3)*3;
      int col_start = (c/3)*3;
      for(i=row_start;i&lt;row_start+3;i++)
      {
          for(j=col_start;j&lt;col_start+3;j++)
          {
              if(matrix[i][j]==n)
                  return 0;
          }
      }
      return 1;
  }
  int main(int argc, char * argv[]) {
      int pipe_descs[2];
      int matrix[SIZE][SIZE];
      int fdr, fdw;   // file descriptors
      int i,j;
  /*  if (pipe(pipe_descs) == -1) {
          fprintf(stderr, "cannot open");
          exit(1);
      }
      pid_t status = fork();
      if(status ==0 )
      {
  
      }*/
      fdr = open(argv[1], O_RDONLY);   // open files
      fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
      if (fdr &lt; 0 || fdw &lt; 0) { //validation for error
          perror("failed to open input or output files");
          exit(EXIT_FAILURE);
      }
      removeSpaces(matrix,fdr, fdw);
      for(i=0; i&lt;9; i++){    /* Iterate of each row */
          for(j=0; j&lt;9; j++){    /* In each row, go over each col element  */
            printf("%c ",matrix[i][j]); /* Print each row element */
          }
          printf("\n");        /* Finish a row, start a new line */
        }
      close(fdr);   // close the files
      close(fdw);
  
      exit(EXIT_SUCCESS);
  }
  </code></pre>