I tried to write basic program in C which copy data from file to another with given source path, destination path and buffer size as input. my problem is the destination file filled with junk or something because its way larger than the source (get bigger depending on buffer size) and can't be open. How do i read and write just the bytes in the source? i'm working in linux, and this is the actually copying part:
char buffer[buffer_size];
int readable=1;
int writeable;
while(readable != 0){
readable = read(sourcef, buffer, buffer_size);
if(readable == -1){
close(sourcef);
close(destf);
exit_with_usage("Could not read.");
}
writeable = write(destf, buffer, buffer_size);
if(writeable == -1){
close(sourcef);
close(destf);
exit_with_usage("Could not write.");
}
}