linux - Problem executing fread function
伊谢尔伦
伊谢尔伦 2017-06-07 09:24:30
0
2
1261

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
滿天的星座

The function of

strlen is to traverse a char*, if it finds 0x00 (the so-called end character '

伊谢尔伦

C language

int strlen(char *p){
    int c=0;
    while(*(p++)) c++;  // p[c] != 0
    return c; 
}
And the result read by
rb can be considered as an array of uint8

, that is, an array of

0~255. For JPEG, you should know that the first field of APP0 is the length of this field 0x00,0x10

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template