Domanda

This is a very small portion of a large project...

These are typedefs defined in standard header file for the project.

typedef uint16_t u_int16_t;
typedef uint32_t u_int32_t;
typedef uint8_t  u_int8_t;

Now this is the actual function causing problem ...

void function(u_int8_t *data1, u_int32_t data1len,
    u_int8_t *data2, u_int32_t data2len)
{

FILE *fq,*fr,*fs;
    char *data3;
    int data3len;

    data3len=data1len+data2len;

    printf("\n%d",data1len);
    printf("\n%d",data2len);
    printf("\n%d",data3len);

fq=fopen("data1.txt","wb");
fwrite((char *)data1,data1len,1,fq);

fr=fopen("data2.txt","wb");
fwrite((char *)data2,data2len,1,fr);

data3=(char *)data1;
strcat(data3,(char *)data2);

fs=fopen("data3.txt","wb");
fwrite((char *)data3,data3len,1,fs);

}

Some output Snapshots ...

40
14
54


 udit@udit-Dabba ~$ hexdump -C data1.txt
 00000000  60 00 00 00 00 8c 06 00  00 00 00 00 00 00 00 00  |`...............|
 00000010  00 00 00 00 00 00 00 01  00 00 00 00 00 00 00 00  |................|
 00000020  00 00 00 00 00 00 00 02                           |........|
 00000028

 udit@udit-Dabba ~$ hexdump -C data2.txt
 00000000  00 26 00 26 00 00 00 01  00 00 00 02 34 12 00 65  |.&.&........4..e|
 00000010  00 34 00 00 61 62 63 64                           |.4..abcd|
 00000018

 udit@udit-Dabba ~$ hexdump -C data3.txt
 00000000  60 00 00 00 00 8c 06 00  00 00 00 00 00 00 00 00  |`...............|
 00000010  00 00 00 00 00 00 00 01  00 00 00 00 00 00 00 00  |................|
 00000020  00 00 00 00 00 00 00 02  00 78 f8 65 00 00 00 02  |.........x.e....|
 00000030  f4 1f 96 00 18 34 a6 bf  1c 03 96 00 88 f1 90 08  |.....4..........|
 00000040

Why contents of data2.txt are not copied to data3.txt ??? If there is any other possible way then please tell me !!!! Thanx in advance ...

È stato utile?

Soluzione

I think you could do it much easier, saving you from ensuring that the respective memory block is large enough.

void function(u_int8_t *data1, u_int32_t data1len,
    u_int8_t *data2, u_int32_t data2len)
{
    FILE *fq,*fr,*fs;
    char *data3;

    fq=fopen("data1.txt","wb");
    fwrite((char *)data1, data1len, 1, fq);
    fclose(fq);

    fr=fopen("data2.txt","wb");
    fwrite((char *)data2, data2len, 1, fr);
    fclose(fr);

    fs=fopen("data3.txt","wb");
    fwrite((char *)data1, data1len, 1, fs);
    fwrite((char *)data2, data2len, 1, fs);
    fclose(fs);
}

Altri suggerimenti

Why contents of data2.txt are not copied to data3.txt?

strcat is specifically for concatenating C strings and only copies up until the null terminator character. So it stops copying from the source once it encounters 00 which it considers the end of the string. Notice how data2 begins with a 00, so it stops immediately.

You'll instead need to memcpy with the destination being 1 past the last byte in data3 and the source being data2. You'll also need to fail gracefully if data3 (which is really data1) does not have enough space to hold data2.

You try to append data2 to the end of data1. You intend to put the result into a third string (data3), but in fact data3 is just data1; they point to the same location. Your code is equivalent to

 strcat (data1, data2);

which of course writes beyond data1's end. (there is another problem: if the caller of the function uses a "string_literal" as argument#1, data1 will not be writable. )

You probably have to allocate some memory for the resulting string and make point data3 to it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top