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
The function of
strlen
is to traverse achar*
, if it finds0x00
(the so-called end character'
C language
And the result read by
, that is, an array ofrb
can be considered as an array of
uint80~255
.
For JPEG, you should know that the first field of
APP0is the length of this field
0x00,0x10