I have an assignment due in 2 days and I've hit a bit of a wall. I have a client program which receives parts of a file from multiple servers and combines them into the original file. The client can receive the files without a problem but wont combine them as when fopen is called it seems to return an empty file.

int combineFiles(char * filename, int parts){
    FILE *writeFile, *readFile;
    char partName[256];
    int i, readAmount;
    char buffer[1024];
    
    if((writeFile = fopen(filename, "wb")) == NULL){
        return -1;
    }
    
    for(i = 1; i <= parts; i++){
        sprintf(partName, "%s.part.%d", filename, i);
        
        if((readFile = fopen(partName, "rb")) == NULL){
            fclose(writeFile);
            return -1;
        }

        do{
            if((readAmount = fread(buffer, sizeof(char), PACKETSIZE, readFile)) < 0){
                fclose(writeFile);
                fclose(readFile);
                return -1;
            }
            fwrite(buffer, sizeof(char), readAmount, writeFile);
        }while(readAmount > 0);
        
        fclose(readFile);
    }
    
    fclose(writeFile);
    
    return 0;
}

Because the program thinks the files are empty it won't write to the new one. I've checked the files and they all exist and are not empty. fgets on the file results in a blank string. I have no idea as to what is going on. Any ideas?

Turns out when I downloaded the files from the server I forgot to close them, so calling fopen again was a bit redundant. It also meant I was starting to read at the end hence the seemingly false eof.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.