C programming assignment: Assignment Details: You are supposed to write a small
ID: 3681189 • Letter: C
Question
C programming assignment:
Assignment Details:
You are supposed to write a small program, which takes two arguments on the command line:
the name of a source file and
the name of a destination file.
For the ease of checking the result, let’s assume that the source file is an ASCII file. Your program copies the contents of the source file into the destination file, but with a catch: the destination file will be an exact byte-by-byte copy of the original file, but IN REVERSE BYTE ORDER. I want you, however, to implement the "reversing" part in a certain way. Specifically, you are required to read the source file starting at its end, block-wise, and write the reversed blocks into the destination file. All blocks are supposed to have a constant, predefined (and small) size - but not size of one(!). I suggest using a #define preprocessor directive, e.g. #define BLOCK_SIZE 16
Needless to say, the code for reversing the contents of the original file needs to be done using pointers when ever appropriate.
The system calls needed to make this assignment work include: open(), read(), write(), lseek() and close(). Assignments that use either of the FILE oriented calls fopen, fread, fwrite, fclose will not be graded.
Hints
The biggest challenge is probably to deal with the "overflow", i.e. the remaining bytes that do not fit into a full block. You should convince yourself that it would help a lot if you knew the size of the file to figure out that overflow before you even get into the details of reversing characters. I suggest you google for system calls that allow you to do that.
End assignment details
Thank you for the help
Explanation / Answer
Please find below the program which will help you to achive this task:
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int argc, char *argv[]) {
int source, dest, n;
char buf;
int filesize;
int i;
if (argc != 3) {
fprintf(stderr, "usage %s <source> <dest>", 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;
}
FILE A File B will be
|------------| |-------------|
|ABCDEF| |FEDCBA |
|------------| |-------------|
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.