After checking for an answer here, and running it in my environment I found that the code still runs into the same issues as my code. This issue is that whenever I have an input file similar to this...
FILE A
|---------------------|
| ABCDE |
| abcde |
|---------------------|
I get extra newline white space generated in my destination file.
FILE B
|---------------------|
| edcba |
| |
| EDCBA |
|---------------------|
After debugging my code I could see that '\n' was being copied into the destination file twice, I'd like to understand why lseek is doing this.
Attached is my code. The critical section in question is in the do/while loop. I'm pretty sure my thought process is sound, as the code from this answer that I looked up would output the exact same results.
#define CHAR_SIZE 2048
#define BUFF_SIZE 1
#define PERMS 0666
int main(int argc, char* argv[]){
if (argc < 3) {
return 1;
printf ("ERROR: not enough arguments\n");
}
int src_file = 0; int dest_file = 0;
int n = -1;
if((src_file=open(argv[1],O_RDONLY)) < -1) return 1;
if((dest_file = creat(argv[2], PERMS)) < -1) return 1;
printf ("The filesize is: %d\n", lseek(src_file, (off_t) 0, SEEK_END));
char buffer[BUFF_SIZE];
lseek (src_file,n--,SEEK_END);
do {
read(src_file,buffer,BUFF_SIZE);
printf ("%s", buffer);
write(dest_file,buffer,BUFF_SIZE);
}while (lseek (src_file,n--,SEEK_END) > -1);
printf("\n");
return 0;
}