09-19-2010 05:11 AM
Hello,
I have such a code phase:
static char data[]="Start to read";
int SaveLogData(char *data)
{
    FILE *fp;
    size_t len;
    
    len = sizeof(data)+1;
    
    fp = fopen("..\\log.dat", "a");
    fwrite(data, len*sizeof(char), 1, fp);
    fwrite("::", 3*sizeof(char), 1, fp);
    fwrite(DateStr(), 10*sizeof(char)+1, 1,fp);
    fclose(fp);
    return 0;
}
The compiling mode is in 64-bit, from the "log.dat" file, I can just see the part of the input string:"Start to r", I don't know why it will truncate some characters. Please advise how to avoid such a fault.
David
09-20-2010 05:13 AM
Within your SaveLogData() function, you are re-defining data as a char*, instead of the original global static char[]. This causes the original array to become hidden, or out of scope, from the function. So the sizeof(data) operation within this function will return 8, which is the size of the 64 bit pointer you are passing to the function.
It is unwise to re-define variables in this way - it leads to confusion! Depending on your application, maybe you could use strlen(data) instead?
JR
09-24-2010 06:50 PM
Hi JR,
Yes , using the strlen() can resolve it. Thanks.
David