0

I'm new on stackoverflow ! I have a problem with my code. I would have liked to know how can I improve my code so that it copies all kinds of files without error? For example, only .log files get copied without worry. But I would like to copy also the files .JPG, .zip, .... When I try with this kind of extension, the copy always gets an error. Thank you in advance for your answers

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX_CHAR 255

int main(int argc, char * argv[])
{
    FILE * sourceFile = fopen(argv[1], "r");    
    FILE * destFile = fopen(argv[2], "w");    
    char line[MAX_CHAR];

    while (fgets(line, MAX_CHAR, sourceFile) != NULL)
    {
        fputs(line, destFile);
    }

    fclose(sourceFile);
    fclose(destFile);
}
Sploon
  • 11
  • 1

1 Answers1

2

The problem is that binary files typically have strings of bytes larger than 255 bytes before a null character. Processing binary files as text simply does not work. Do not use the string functions.

ojblass
  • 21,146
  • 22
  • 83
  • 132