zoukankan      html  css  js  c++  java
  • 以二进制方式读写jpg文件

    long _filesize(FILE *stream) 
    { 
        long curpos, length; 
        curpos = ftell(stream); 
        fseek(stream, 0L, SEEK_END); 
        length = ftell(stream); 
        fseek(stream, curpos, SEEK_SET); 
        return length; 
    } 
    
    char* readBinFile(char* name,int& nSize)
    {
        FILE *fp;
        if ((fp=fopen(name, "rb")) == NULL)
            return NULL;
        long buffsize = _filesize(fp);
        nSize = buffsize;
        char *buff = (char *)malloc(buffsize*sizeof(char));
        //printf("1--%d
    ",buffsize);
        fread(buff, buffsize, 1, fp);
        fclose(fp);
        return buff;
    }
    int writeBinFile(char* name,char* buf, long nSize)
    {
        FILE *fp = fopen(name,"wb");
        if (fp == NULL)
        {
            printf("无效文件句柄
    ");
            return -1;
        }
        fwrite(buf,nSize,1,fp);
        //printf("2--%d
    ",nSize);
        fclose(fp);
        return 0;
    }

    使用例子

    int nSize = 0;//保存jpg二进制数据的大小
    char* buf = readBinFile("111.jpg",nSize);
    if (buf == NULL)
    {
        printf("打开文件失败
    ");
    }
    writeBinFile("112.jpg",buf,nSize);//简单测试,未对返回值做校验
    //释放申请的内存
    if (buf)
    {
        free(buf);
        buf = NULL;
    }
  • 相关阅读:
    省市级联
    innerText与innerHTML的区别
    使用代码提交表单
    Date的使用
    Array(数组)的使用
    [贪心]Rest Stops
    [数论][Miller_Rabin] Goldbach
    [简单思维题]Hoofball
    [数论][容斥原理]Co-prime
    [图论][二分图最大匹配]Courses
  • 原文地址:https://www.cnblogs.com/LYF-LIUDAO/p/7903879.html
Copyright © 2011-2022 走看看