-2

hello guys i made a program which copying binary files, but i have a problem: when im trying to copy the field of the source file to the target file its removing all the field of the target file. how can i solve this problem?

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

int  main(int argc, char** argv)
{
    FILE  *source, *target;
    const size_t buffer_size = 8192;
    char buffer[100];
    int strint[100];
    int  a;

    source = fopen(argv[1], "rb");


    if (source != NULL)
    {
        return  -1;
    }

    target = fopen(argv[2], "w+b");

    while (1)
    {
        a = fread(buffer, sizeof(char), buffer_size, source);

        if (!feof(source))
        {
            fwrite(buffer, sizeof(char), buffer_size, target);
        }
        else
        {
            break;
        }

    }

    fclose(target);
    fclose(source);
    system("pause");
    return 0;

}

3 Answers3

1
  1. You declare buffer_size to be 8192, but you only allocate 100 chars instead of buffer_size. So change the declaration. Also, constants are typically all-caps.

  2. You need to write a bytes onto the destination file, not buffer_size, because the file may not even be buffer_size characters!

  3. Check the return value of fopen; it returns NULL on failure. Check out strerror to print an error reason.

  4. Your loop is wrong. Simply loop while fread returns BUFSIZE (that is, fread reads BUFSIZE characters).

  5. Never use system, because it is highly unportable.

The revised program is

FILE *Fopen(const char *filename, const char *mode)
{
    FILE *fp;

    if ((fp = fopen(filename, mode)) == NULL) {
        strerror("fopen");
        exit(EXIT_FAILURE);
    }
    return fp;
}
int main(int argc, char **argv)
{
    const size_t BUFSIZE = 8192;

    FILE *src, *dest;
    char buf[BUFSIZE];
    size_t n;

    src = Fopen(argv[1], "rb");
    dest = Fopen(argv[2], "w+b");

    while ((n = fread(buf, 1, BUFSIZE, src)) > 0)
        fwrite(buf, 1, n, dest);

    fclose(src);
    fclose(dest);
    return 0;
}

I have not tested the code, so tell me if something is wrong.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
0
const size_t buffer_size = 8192;
char buffer[100]; // 100 should be buffer_size instead

You're not allocating your buffer size for buffer

Also, you're potentially writing the wrong number of bytes:

        fwrite(buffer, sizeof(char), buffer_size, target);

you should be writing a bytes back so swap out buffer_size with a

xaxxon
  • 19,189
  • 5
  • 50
  • 80
0

Among other issues noted, If by "its removing all the field of the target file" you mean the file target file has all contents removed and you don't want that, you need to read the man page, because it says:

w+

Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

Thus,

target = fopen(argv[2], "w+b");

removes all pre-existing data from the target file when you open it.

how can i solve this problem?

Open the file in append mode:

a

Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

like this:

target = fopen(argv[2], "ab");

Only use

target = fopen(argv[2], "a+b");

if you also want to read from target.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56