0

我试图在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 将它们写入一个新文件中。读写都是受控信号量......


4

0 回答 0