Για παράδειγμα η παρακάτω συνάρτηση αντιγράφει ένα αρχείο σε ένα άλλο (Unix):
/*
* Copy file specified by path1 to path2
* Return 0 on success -1 on error
* (dds)
*/
int
copyfile(char *path1, char *path2)
{
char buf[512]; /* Copy buffer */
int ifd, ofd; /* File descriptors */
int nrbytes; /* Bytes read */
int nwbytes; /* Bytes written */
struct stat sbuf; /* Attribute buffer */
/* Get attributes of source file */
if (stat(path1, &sbuf) == -1)
return (-1);
/* Open source file */
if ((ifd = open(path1, O_RDONLY)) < 0)
return (-1);
/* Create destination file */
if ((ofd = creat(path2, sbuf.st_mode & 0777)) < 0) {
close(ifd);
return (-1);
}
/* Copy source to destination in chunks */
while ((nrbytes = read(ifd, buf, sizeof(buf))) > 0) {
if ((nwbytes = write(ofd, buf, nrbytes)) != nrbytes) {
nrbytes = -1;
break;
}
}
/* Close source file */
if (close(ifd) < 0) {
close(ofd);
return (-1);
}
/* Close destination file */
if (close(ofd) < 0) {
return (-1);
}
/* Success! */
return (0);
}