After successfully executing the fread function, the returned value is equal to data_len. But after executing strlen, the length of the string buff is wrong. But I used fwrite to write the picture to another .jpg and it was OK
int main()
{
FILE *fp,*out;
int c,len;
out=fopen("out.jpg","wb");
if((fp=fopen("/home/jens/Desktop/picture.jpg","rb"))<0){
printf("error open\n");
}
if(fp == NULL)
printf("Open file Error!");
fseek(fp,0,SEEK_END);
int data_len=ftell(fp);
printf("datalen %d",data_len);
rewind(fp);
char* buff=(char *)malloc((data_len)*sizeof(char)+1);
size_t a=fread(buff,sizeof(char),data_len,fp);
printf( "\n a %d",a); //a的值是图片的大小
len=strlen(buff);
printf("\n len %d",len);//在这里buff的大小却只是4。
char* HTTP_INFO=(char *)malloc((data_len)*sizeof(char)+1024);
char *buf="abcdefg";
printf("\n %d",strlen(buf));
len=sprintf(HTTP_INFO,"%s",buf);//如果把这个换成buff,结果也还是4。
printf("\nlen %d",len);
fclose(fp);
fclose(out);
return 0;
}
The running result is as shown below
滿天的星座2017-06-07 09:26:30
The function of
strlen
is to traverse a char*
, if it finds 0x00
(the so-called end character '
伊谢尔伦2017-06-07 09:26:30
C language
And the result read by int strlen(char *p){
int c=0;
while(*(p++)) c++; // p[c] != 0
return c;
}
rb can be considered as an array of
uint8
0~255.
For JPEG, you should know that the first field of
APP0 is the length of this field
0x00,0x10