I have a problem copying a file from a directory to another in C. Specifically, my code works well with textual files but doesn't with executable ones. The number of bytes written is correct and so are the file rights, but the result type of the copy is not recognized as executable by the file system (I'm working on a xubuntu Virtual Machine) and has "Unknown" type. Therefore, if I echo the results of md5sum from command line on bash, they are different.
In the following code, "checkErrno" is a function defined by me that calls perror. "currDir" is the current directory and "DIRNAME" is the directory I want to move to. All the libraries are correctly included, I just copied and pasted the chunk of code that matters.
struct stat fileSt;
if(stat(fileName,&fileSt) != 0) checkErrno("Stats file");
char currDir[PATH_MAX];
FILE* file;
if(getcwd(currDir,PATH_MAX) == NULL) checkErrno("Currdir");
if(chdir(DIRNAME) != 0) checkErrno("Chdir");
if((file = fopen(fileName,"w")) == NULL) checkErrno("Create file");
if(chmod(fileName, fileSt.st_mode & 07777)) perror("chmod");
if(fwrite(fileMsg.data.buf,1,fileSize,file) < fileSize) checkErrno("fwrite");
fflush(file);
if(fclose(file) != 0) checkErrno("fclose");
if(chdir(currDir) != 0) checkErrno("chdir");
Thanks in advance!
EDIT: The mistake was in the function I used to read the file. Once I found out I easily solved it.