Problem with the memcpy

Hi all,I am facing problem with the memcpy function while copying

for (i = 0; i <= ((128*1024)-len); i++) {
    if (memcmp(buf + i, search_data,len) == 0) {
        memcpy(buf + i, &replace_data, len);
    }
}

Upto some value the data is copying then the memcmp function fails.I dnt know why…?
buf is char pointer and search_data and replace_data char array of len-32.
here the len is 8.

Anything wrong in this ,please advice

Here is the code:

=====================================================

void update(char `*`device,unsigned long pos)
{
    char search_str[MAX_LEN]="", replace_str[MAX_LEN]="";
    unsigned char search_data[32]="ffbff9e0", replace_data[32]="ffbff9c0";
    unsigned long i;
    FILE `*`fp;
    char `*`buf;
    fp=fopen(device,"rb+");
    fseek(fp, pos, SEEK_SET);
    buf=(char `*`)calloc(1, (128 `*` 1024));
    fread(buf,(128 `*` 1024),1,fp); 
    for (i = 0; i <= ((128 * 1024) - strlen(search_data)); i++)
    {
        if (memcmp(buf + i, search_data, strlen(search_data)) == 0)
        {
            memcpy(buf + i, &replace_data, strlen(search_data));
        }
    }
    fseek(fp, pos, SEEK_SET);
    fwrite(buf, (128 * 1024), 1, fp);
    fclose(fp);
    free(buf);
}

Please give the link to the whole piece of code where all arrays are defined. Or post it here but use construction

...
to embrace your code. It is impossible to read it otherwise.

why using &replace_data ? replace_data is already a pointer.

replace_data is char array.

memcmp is failing ,Could anyone tell why this happens…?
Thanks in advance.

if you do the correction i told you, it works as expected. by the way, you could use the strstr() function of the C standard library that does almost the exact same thing:

p = strstr(buf, search_data);
memcpy(p, replace_data, strlen(replace_data));

Thanks for the reply , I will try it.

Hi ,I tried with strstr it’s giving the Segfault.
If possible can you post me the program for copying blocks of data into memory location.Thanks in advance…!!

It should do the job :

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

int main()
{
    char buf[1024], *p;
    strcpy(buf, "dquysoqdu ffbff9e0 sqidynsiydb");
    printf("%s\n", buf);
    p = strstr(buf, "ffbff9e0");
    memcpy(p, "ffbff9c0", 8);
    printf("%s\n", buf);
    return 0;
}