I am trying to transfer files over a TCP connection, and I noticed that binary/executable files on Mac don't have file extensions. This doesn't seem to be a problem when reading from an existing binary file, but when trying to write to a new one, it creates a blank file with no extensions-nothing. How can I fix this? Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char* filename = "helloworld";
FILE* file = fopen(filename, "rb");
FILE* writefile = fopen("test", "wb");
fseek(file, 0, SEEK_END);
unsigned int size = ftell(file);
printf("Size of %s is: %d bytes\n", filename, size);
fseek(file, 0, SEEK_SET);
char* line = (char *) malloc(size+1);
fread(line, size, 1, file);
fwrite(line, size, 1, writefile);
free(line);
fclose(writefile);
fclose(file);
return 0;
}
helloworld
is the existing executable I'm reading from (which is working) and I'm trying to write to a new executable which would be called test
Your code looks fine (ignoring lack of error checking). You'll need to add x
(executable) permission when the copy is done.
From the terminal, you can type chmod +x test
.
From within the program:
#include <sys/types.h>
#include <sys/stat.h>
...
fclose(writefile);
fclose(file);
chmod("test", S_IRWXU);
return 0;
}