>but I have been told that reading byte to byte will kill performance
You shouldn't listen to people who overgeneralize, especially when they have no idea what they're talking about. Since they were probably thinking about fread and fwrite, both methods would rely solely on the buffering of the stream for performance. fread successively calls fgetc, and fwrite successively calls fputc.
In my implementation of stdio, there was no noticeable peformance difference between the fgetc/fputc loop and the equivalent code using fread and fwrite because any device I/O was avoided until the buffer filled itself, and device I/O is the big hitter when it comes to performance in I/O libraries.
>shouldn't I be using an FEOF instead of EOF?
No, FEOF doesn't exist. There is an feof function, but beginners to C have a hard time using it correctly.
>I'm still totally confused
Get used to it. These days, I'm still confused, but it's a whole new level of confused.
>I don't understand how to dump data into a buffer and know how to detect the end of each record??
Now, you didn't say anything about individual records. Your words were "variable length file", with no mention of records. If you want a good answer, you need to ask a question accurate to your problem. But, if you want to copy blocks, you can do it like this:
Code:
char block[1024];
size_t n;
while ( ( n = fread ( block, 1, sizeof block, in ) ) == sizeof block ) {
if ( fwrite ( block, 1, sizeof block, out ) != sizeof block ) {
/* Write error */
}
}
if ( feof ( in ) ) {
/* Copy the last block */
if ( fwrite ( block, 1, n, out ) != n ) {
/* Write error */
}
}
else {
/* Read error */
}
When you get into separating the file into records, you have to format the file so as to make it easy. For example, if the file is written so that the byte count of each record is prepended to the record, you can read the file as you would in COBOL using fread as shown above.