danielwinzard
danielwinzard

Reputation: 33

how to copy binary files in c

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;
}

Upvotes: 1

Views: 2657

Answers (1)

Sandro Kalatozishvili
Sandro Kalatozishvili

Reputation: 602

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;
}

Upvotes: 2

Related Questions