gcc - Copy file, but error double free or corruption in C -


int main(int argc, char **argv) {     file *fe, *fs;     unsigned char buffer[2048];      int bytesreader;      fe = fopen(argv[1], "rb");       fs = fopen(argv[2], "wb");        while((bytesreader = fread(buffer, 1, 2048, fe)))        fwrite(buffer, 1, bytesreader, fs);      fclose(fe);     fclose(fs);     return 0; } 

and error is...

*** glibc detected *** ./doc2: double free or corruption (top): 0x096b7008 *** 

sorry, correct, dont undertand. fail?

you need check return values of fopen() 0.

calling fclose() on 0 results in error seeing.

also, check argc valid range of arguments.

int main(int argc, char **argv) {     file *fe, *fs;     unsigned char buffer[2048];      int bytesreader;      if(argc != 3) {         fprintf(stderr, "usage: %s file1 file2\n", argv[0]);         exit(1);     }      fe = fopen(argv[1], "rb");      if(fe == 0) {         fprintf(stderr, "%s: failed open %s read\n", argv[0], argv[1]);         exit(1);     }      fs = fopen(argv[2], "wb");      if(fs == 0) {         fprintf(stderr, "%s: failed open %s write\n", argv[0], argv[2]);         exit(1);     }       while((bytesreader = fread(buffer, 1, 2048, fe)))        fwrite(buffer, 1, bytesreader, fs);      fclose(fe);     fclose(fs);     return 0; } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -