title: Copying files error in C
body: <p>Im trying to copy files in c. I have no problems copying .txt, but with files like .jpeg i get errors... help pls</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #define BUFFER_SIZE 20
  
  pthread_mutex_t mutex;
  pthread_cond_t condP;
  pthread_cond_t condC;
  
  int varProductor=0;
  int varConsumidor=0;
  int cantVar=0;
  
  FILE *fileP;
  FILE *fileC;
  
  char bytes[BUFFER_SIZE];
  char var;
  
  
  
  void* funcProductor(){
      while(!feof(fileP)){        
          pthread_mutex_lock(&amp;mutex);
  
          while(cantVar==BUFFER_SIZE){                    
              pthread_cond_wait(&amp;condP,&amp;mutex);
          }
  
  
          if(!feof(fileP)){       
              bytes[varProductor]=getc(fileP);
              varProductor=(varProductor+1)%BUFFER_SIZE;          
              cantVar++;
          }
          pthread_mutex_unlock(&amp;mutex);   
          pthread_cond_signal(&amp;condC);
  
      }   
  }
  void* funcConsumidor(){
      while((varProductor-1)!=varConsumidor){
          pthread_mutex_lock(&amp;mutex);         
  
          while(cantVar==0){
              pthread_cond_wait(&amp;condC,&amp;mutex);
          }
          if((varProductor-1)!=varConsumidor){    
              putc(bytes[varConsumidor],fileC);
              varConsumidor=(varConsumidor+1)%BUFFER_SIZE;
              cantVar--;
          }
  
          pthread_mutex_unlock(&amp;mutex);   
          pthread_cond_signal(&amp;condP);    
  
      }
  }
  
  int main(int args,char** argv){ 
      int hilosOrigen=atoi(argv[2]);
      int hilosDestino=atoi(argv[4]);
  
  
  
      fileP=fopen(argv[1],"rb");
      fileC=fopen(argv[3],"wb");
  
      pthread_mutex_init(&amp;mutex,NULL);
      pthread_cond_init(&amp;condP,NULL);
      pthread_cond_init(&amp;condC,NULL);
  
      pthread_t threadP[hilosOrigen];
      pthread_t threadC[hilosDestino];
  
      int i;
  
      for(i=0;i&lt;hilosOrigen;i++){
          pthread_create(&amp;threadP[i],NULL,funcProductor,NULL);
      }
      for(i=0;i&lt;hilosDestino;i++){
          pthread_create(&amp;threadC[i],NULL,funcConsumidor,NULL);
      }
      for(i=0;i&lt;hilosOrigen;i++){
          pthread_join(threadP[i],NULL);
      }
      for(i=0;i&lt;hilosDestino;i++){
          pthread_join(threadC[i],NULL);
      }
  
  
      return 0;
  }
  </code></pre>
  
  <p>*Im using pthreads to read bytes and saving them into and array, then more pthreads write them in a new file. The reading and writing are controlled Semaphores.....</p>
  
  <hr>