Mega Code Archive

 
Categories / C / Linux
 

Appending two files

#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char **argv) { char buf[1024]; int n, in, out; if(argc != 3) { fprintf(stderr, "Usage: append SOURCE TARGET\n"); return 0; } /* open the first file for reading */ in = open(argv[1], O_RDONLY); /* the second file for writing */ out = open(argv[2], O_WRONLY|O_APPEND); /* copy data from the first file to the second file */ while((n = read(in, buf, sizeof(buf))) > 0) write(out, buf, n); return 0; }