Question
Write a program which will copy a file in reverse (resulting file will have the same contents as the original file only in reverse order). Your program should work on any file and use any size buffer.
Explanation / Answer
#include #include #include #include #include #include int main(int argc, char *argv[]) { int source, dest, n; char buf; int filesize; int i; if (argc != 3) { fprintf(stderr, "usage %s ", argv[0]); exit(-1); } if ((source = open(argv[1], 0400)) < 0) { //read permission for user on source fprintf(stderr, "can't open source"); exit(-1); } if ((dest = creat(argv[2], 0700)) < 0) { //rwx permission for user on dest fprintf(stderr, "can't create dest"); exit(-1); } filesize = lseek(source, (off_t) 0, SEEK_END); //filesize is lastby +offset printf("Source file size is %d ", filesize); for (i = filesize - 1; i >= 0; i--) { //read byte by byte from end lseek(source, (off_t) i, SEEK_SET); n = read(source, &buf, 1); if (n != 1) { fprintf(stderr, "can't read 1 byte"); exit(-1); } n = write(dest, &buf, 1); if (n != 1) { fprintf(stderr, "can't write 1 byte"); exit(-1); } } write(STDOUT_FILENO, "DONE ", 5); close(source); close(dest); return 0; }