Im trying to read file in binary mode using C, but it only stores in the buffer the first 43 characters. I want to read the file in groups of 245 bytes. It contains multicharacter bytes and also null chars. This is the content of the file in hex:
323031353037303735393036333130343739332032373231333732534e30323033323545533036303130340000000008557c0000000000693c0000000000000c0000000008557c0000000000693c0000000000000c0000000008557c0000000000693c0000000000000c00001c00001c00001c00000c00000c00000c00001c4d4e202020204942202020204f393920202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202039444b524d4144
And this is the code that I have:
char* to_hex(const char* strin) {
char * strout = malloc(2 * strlen(strin) + 1);
int x;
for (x = 0; x < strlen(strin);x++){
sprintf(&strout[x+x],"%02x", (int)(*(unsigned char*)(&strin[x])) );
}
strout[2 * strlen(strin)]='\0'
return strout;
}
int main(int argc, char *argv[]) {
FILE * pfinput = fopen("stack.bin", "rb");
int lrec = 245;
char* sbuff = (char *)malloc((lrec + 1) * sizeof(char));
if (pfinput != NULL) {
while (fread (sbuff, 1, lrec, pfinput) > 0){
sbuff[lrec] = '\0';
printf("len=%d hex=%s\n\n", strlen(sbuff), to_hex(sbuff) );
// here i want to work with the string.
// lencampo is would has a variable value depending of the loop.
//
// campo = (char *)malloc((lencampo + 1) * sizeof(char));
// memcpy(campo, &sbuff[lstart], lencampo);
// campo[lencampo + 1]='\0';
// sqlite3_bind_blob(statement, loffset, campo, (lencampo + 1), SQLITE_STATIC);
// but it only inserts 43 chars too...
}
}
return 0;
}
It returns the following:
len=43 hex=323031353037303735393036333130343739332032373231333732534e3032303332354553303630313034
Why it only reads 43 characters instead of 245? Do you have any alternative to do it? Thank you.
Aucun commentaire:
Enregistrer un commentaire