Copy a binary file in another binary file

Hi!

Firstly, I'm french and I don't speak and write very well english langage. So sorry for the faults of langage.

Recently, i'm in intership in a company and, I'm stopped in this problem that I will expose you.
I have to copy a binary file in another binary file, in suppress the 8 first octets of my source file. And I'm not allowed to use dynamic variable, Ihave to use POINTER.

And my big problem is that, I have some difficulties to use pointer, and when I use the function "fwrite" to copy informations source file to the Target File, I have an error message: "out-of bounds pointer artmetic:5bytes(5elements) past end of array"

So if someone can telle me what I might forget or suppresss in my program.



char TargetFile[MAX_PATHNAME_LEN];
char TargetFile0[MAX_PATHNAME_LEN];
char TargetFile1[MAX_PATHNAME_LEN];
char TargetFile2[MAX_PATHNAME_LEN];
char ligne[1000];
char ligne1[1000];

int etat1=0;
int i=0,j=0;

FILE *FileSrc;
FILE *FileSrcDos; //en Dos
FILE *FileBin;
FILE *FileBin1; // sans les 8 octets

char *Buffer,*Buffer1;
int size_file=0;
.
.
.
.


sprintf(TargetFile2,"%s.bin",TargetFile1);
strcat(TargetFile1,"_nouv.bin");



Buffer=(char*)malloc(sizeof(Buffer));


FileBin1=fopen(TargetFile1,"ab");
FileBin=fopen(TargetFile0,"rb");


fread(Buffer,1,sizeof(Buffer),FileBin);


if (fseek(FileBin,0,SEEK_END) != 0)
{
fclose(FileBin);
}

size_file=ftell(FileBin);


fwrite((&Buffer[8]),1,size_file -8, FileBin1);


free(Buffer);
fclose(FileBin);
fclose(FileBin1);]

It's a part of my code.
Last edited on
Instead of sizeof(Buffer) (which will only give the size of the pointer itself),

I think you should be using size_file in both malloc() and fread()

Don't forget to reset the file position back to the start (maybe use rewind() ) after finding the length.

In the fwrite() instead of (&Buffer[8]) maybe something like Buffer + 8
Last edited on
It doesn't work, because, we need the function "fseek" before "ftell", and before "fseek", we need "fread", and buffer initalization can't be put after this program lines:

fread(Buffer,1,sizeof(Buffer),FileBin);


if (fseek(FileBin,0,SEEK_END) != 0)
{
fclose(FileBin);
}

size_file=ftell(FileBin);
It doesn't work, because,
we need the function "fseek" before "ftell",
Agreed

and before "fseek", we need "fread",
But how do we know how many bytes to read?

and buffer initalization can't be put after this program lines:
I agree it cannot go after fread.

The way I see things it goes like this:
1. open files
2. seek to end of input
3. get the position which gives the size of the file
4. allocate buffer with size = file size
5. seek to start  ( rewind )
6. read entire file into buffer
7. write from buffer with offset=8 to output file
8. close both input and output files and free the buffer


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name_in[]  = "input.txt";
    char name_out[] = "copy.txt";

    FILE * infile  = fopen(name_in, "rb");
    FILE * outfile = fopen(name_out, "wb");    
    
    int OMIT = 8;    /* Omit first 8 bytes */

    fseek(infile, 0, SEEK_END);
    size_t file_size = ftell(infile);

    printf("file size: %d bytes\n", file_size);

    char * buffer = (char *) malloc(file_size);

    rewind(infile);   /* Rewind needed because of fseek */
    fread(buffer, 1, file_size, infile);
    

    fwrite(buffer + OMIT, 1, file_size - OMIT, outfile);

    fclose(infile);    
    fclose(outfile);
    free(buffer);

    return 0;
}
Last edited on
Alternatively, for large files there may not be sufficient RAM to allocate a buffer to hold the entire file. instead, read / write in blocks.

fseek() here is used to omit the first 8 bytes of the input file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

int main() 
{
    char name_in[]  = "input.txt";
    char name_out[] = "copy.txt";
    
    FILE * infile  = fopen(name_in, "rb");
    FILE * outfile = fopen(name_out, "wb");
     
    char  buffer[1024];
    
    fseek(infile, 8, SEEK_SET); /* omit first 8 bytes */

    size_t count_in;

    /* copy from input to output */
    while (count_in = fread(buffer, 1, sizeof(buffer), infile))
        fwrite(buffer, 1, count_in, outfile);

    fclose(infile);    
    fclose(outfile);
    
    return 0;
}
Last edited on
It works.I was in this problem during 2 days, it's so long!! But know I can continue my work, thanks to you.

Thank you very much!!

Topic archived. No new replies allowed.