在C中复制文件错误

时间:2013-04-06 22:38:34

标签: c

我试图在c中复制文件。我没有问题复制.txt,但是像.jpeg这样的文件我得到错误...帮助请

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#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(&mutex);

        while(cantVar==BUFFER_SIZE){                    
            pthread_cond_wait(&condP,&mutex);
        }


        if(!feof(fileP)){       
            bytes[varProductor]=getc(fileP);
            varProductor=(varProductor+1)%BUFFER_SIZE;          
            cantVar++;
        }
        pthread_mutex_unlock(&mutex);   
        pthread_cond_signal(&condC);

    }   
}
void* funcConsumidor(){
    while((varProductor-1)!=varConsumidor){
        pthread_mutex_lock(&mutex);         

        while(cantVar==0){
            pthread_cond_wait(&condC,&mutex);
        }
        if((varProductor-1)!=varConsumidor){    
            putc(bytes[varConsumidor],fileC);
            varConsumidor=(varConsumidor+1)%BUFFER_SIZE;
            cantVar--;
        }

        pthread_mutex_unlock(&mutex);   
        pthread_cond_signal(&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(&mutex,NULL);
    pthread_cond_init(&condP,NULL);
    pthread_cond_init(&condC,NULL);

    pthread_t threadP[hilosOrigen];
    pthread_t threadC[hilosDestino];

    int i;

    for(i=0;i<hilosOrigen;i++){
        pthread_create(&threadP[i],NULL,funcProductor,NULL);
    }
    for(i=0;i<hilosDestino;i++){
        pthread_create(&threadC[i],NULL,funcConsumidor,NULL);
    }
    for(i=0;i<hilosOrigen;i++){
        pthread_join(threadP[i],NULL);
    }
    for(i=0;i<hilosDestino;i++){
        pthread_join(threadC[i],NULL);
    }


    return 0;
}

*我使用pthreads读取字节并将它们保存到数组中,然后更多pthreads将它们写入新文件中。阅读和写作都是受控制的信号量......


0 个答案:

没有答案