with only that amount of sample code, it's difficult to give a correct answer without some guesswork.
Your problem seems to be that you do not know the amount of valid bytes in your buffers in the writer.
buffer
seems to be an array of char arrays? If yes, you could make it an array of structs instead, for example:
struct buf
{
int used_length;
char b[20];
};
struct buf buffer[20];
Then do as follows:
producer:
buffer[pIndex].used_length = count = fread(buffer[pIndex].b, sizeof(char), sizeof(buffer[pIndex].b), myFile);
consumer:
count = fwrite(buffer[cIndex].b, sizeof(char), buffer[cIndex].used_length, myFile);
This way you will only write as much bytes as you read.