I want to copy the content of the source file to the target file, but I get this warning:
warning: passing argument 4 of ‘fwrite’ from incompatible pointer type [-Wincompatible-pointer-types]
fwrite(target, sizeof(char), targetSize, sourceContent);
If i ignore the warning i get a segmentation fault.
FILE *source = fopen(argv[1], "r");
FILE *target = fopen(argv[2], "w");
if (source == NULL || target == NULL) {
printf("One or both files do NOT exist\n");
abort();
}
fseek(source, 0, SEEK_END);
long sourceSize = ftell(source);
fseek(source, 0, SEEK_SET);
char *sourceContent = (char *)malloc(sourceSize);
fread(sourceContent, sizeof(char), sourceSize, source);
long targetSize = sourceSize;
fwrite(target, sizeof(char), targetSize, sourceContent);
Both fread()
and fwrite()
take the buffer where to read/write as first argument, and the file as fourth.
// This is fine.
fread(sourceContent, sizeof(char), sourceSize, source);
// Swap the first and fourth argument in the fwrite call.
fwrite(sourceContent, sizeof(char), targetSize, target);