1

hello guys i am trying to make a program which which copying binary files (the values) from the source to the target. but i has a mistake and i don't know how to solve that, how can i complete the code?

int main(int argc, char ** argv)
{
    FILE * source, *target;
    int numr, numw;
    char buffer[100];


    source = fopen(argv[1], "rb");
    target = fopen(argv[2], "rb");
    if ((source = fopen(argv[1], "rb")) == NULL)
    {
        printf("open read file error.\n");
        return 0;
    }

    while (feof(source) == 0)
    {
        if ((numr = fread(buffer, 1, 100, source)) != 100)
        {
            if (ferror(target) != 0)
            {
                printf("read file error.\n");
                return 0;
            }
        }
        fwrite(buffer, 1, numr, target);
        if ((numw = fwrite(buffer, 1, numr, target)) != numr)
        {
            printf("write file error.\n");
            return 0;
        }
    }

    fclose(source);
    fclose(target);

    return 0;
}
  • 4
    for one thing I suspect you want to open the target mode `"wb"`... – Steve Summit May 06 '16 at 20:06
  • 3
    It would be nice if you specified, specifically, what error messages you get and where you are getting them. – Fjotten May 06 '16 at 20:09
  • 3
    Also you're opening the source twice (which isn't causing a problem), and writing each block twice (which will cause you quite a problem). – Steve Summit May 06 '16 at 20:12
  • As others have mentioned, you should definitely be opening target "wb". But as far as errors go, you should make sure to use errno: `#include if ((numw = fwrite(buffer, 1, numr, target)) != numr) { printf("write file error: %s\n", strerror(errno)); return 0; } ` – David Bryson May 06 '16 at 20:23
  • 4
    Possible duplicate of [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – n. m. could be an AI May 06 '16 at 20:54
  • [This answer](http://stackoverflow.com/questions/37032360/copying-binary-files-using-fread-and-fwrite/37032916#37032916) shows that the read-write loop can be very short. And there is almost always a better way than the widely misunderstood `feof`. – Weather Vane May 06 '16 at 21:58
  • these two lines are far from correct: `fwrite(buffer, 1, numr, target); if ((numw = fwrite(buffer, 1, numr, target)) != numr)` 1) remove the first line. as that is causing the data to be written twice, when you only want the data to be written once – user3629249 May 08 '16 at 01:08
  • to be blunt, never use `feof()` for loop control. It does not do what you might expect. Suggest using the call to `fread()` to control the loop. – user3629249 May 08 '16 at 01:11
  • regarding this line: `target = fopen(argv[2], "rb");` Since argv[2] is the name for the output file, the line should be: `target = fopen(argv[2], "wb");` so the file is opened for a binary write – user3629249 May 08 '16 at 01:13

1 Answers1

2

At first, you wont open source file twice. Just remove first or second fopen from code.

    source = fopen(argv[1], "rb");
    if (source == NULL)
    {
        printf("open read file error.\n");
        return 0;
    }

Also must open target file with "w" and check it for success.

    target = fopen(argv[2], "a+w");
    if (target == NULL)
    {
        fclose(source);
        printf("open target file error.\n");
        return 0;
    }

Also you dont need to check that fread returned 100, if there is something wrong, ferror() will detect error for you.

        numr = fread(buffer, 1, 100, source);
        if (ferror(target) != 0)
        {
            printf("read file error.\n");
            break;
        }

Also you must use write function once

        numw = fwrite(buffer, sizeof(char), numr, target);
        if (numw != numr)
        {
            printf("write file error.\n");
            break;
        }

I edited your code and now it works fine...

int main(int argc, char ** argv)
{
    FILE *source, *target;
    int numr, numw;
    char buffer[101];

    source = fopen(argv[1], "rb");
    if (source == NULL)
    {
        printf("open read file error.\n");
        return 0;
    }

    target = fopen(argv[2], "a+w");
    if (target == NULL)
    {
        fclose(source);
        printf("open target file error.\n");
        return 0;
    }

    while (feof(source) == 0)
    {
        memset(buffer, 0, sizeof(buffer));
        numr = fread(buffer, 1, 100, source);
        if (ferror(target) != 0)
        {
            printf("read file error.\n");
            break;
        }

        numw = fwrite(buffer, sizeof(char), numr, target);
        if (numw != numr)
        {
            printf("write file error.\n");
            break;
        }
    }

    fclose(source);
    fclose(target);

    return 0;
}
Sun Dro
  • 581
  • 4
  • 9