Also, instead of making the user specify the buffer size, you can do multiple reads/writes until the file is done. Something like:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
FILE *fpin, *fpout;
unsigned char buf[BUFSIZ];
unsigned int cnt;
if(argc != 3)
{
fputs("Usage: mycp <source file> <dest file>\n", stderr);
exit(EXIT_FAILURE);
}
if(!(fpin = fopen(argv[1], "rb")))
{
fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
if(!(fpout = fopen(argv[2], "wb")))
{
fprintf(stderr, "%s: %s\n", argv[2], strerror(errno));
fclose(fpin);
exit(EXIT_FAILURE);
}
while((cnt = fread(buf, 1, sizeof(buf), fpin)))
if(fwrite(buf, 1, cnt, fpout) != cnt)
{
fprintf(stderr, "Error while writing: %s\n", strerror(errno));
break;
}
if(ferror(fpin))
fprintf(stderr, "Error while reading: %s\n", strerror(errno));
fclose(fpin);
fclose(fpout);
return EXIT_SUCCESS;
}