檔案二進位的讀寫

來源:互聯網
上載者:User

讀檔案並以二進位形勢轉到另一建立檔案中

/**//****************************************
*edward nic
*2007.04.25
*
****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys\stat.h>  //for fstst()
int main()
{
    struct stat statBuffer ;
    FILE        *fpin , *fpout;
    char        *p ;
    int         FileSize ;

    fpin = fopen("1.rar", "rb") ;
    fstat(fileno(fpin), &statBuffer) ;
    FileSize = statBuffer.st_size ;
    printf("%d\t", statBuffer.st_size) ;
    
    p = (char *)malloc(FileSize * sizeof(char)) ;

    fread(p, FileSize, 1 , fpin) ;

    fpout = fopen("2.rar", "wb") ;
    fwrite(p, FileSize, 1 , fpout) ;

    free(p) ;
    fclose(fpout) ;
    fclose(fpin) ;
    return 0 ;
}

讀類似“\xff\xaa\x02…”文字檔並還原到二進位檔案
1。把二進位檔案轉\xff\xaa\x02…”文本型式//需要 unsigned char

#include <stdio.h>
#include <stdlib.h>
#include <sys\stat.h>  //for fstst()
int main()
{
    struct stat   statBuffer ;
    FILE          *fpin , *fpout;
    unsigned char *p ;
    int           FileSize ;
    int           i ;
    
    //read data
    fpin = fopen("1.rar", "rb") ;
    fstat(fileno(fpin), &statBuffer) ;
    FileSize = statBuffer.st_size ;
    printf("file size is:%dbytes\n", statBuffer.st_size) ;
    
    p = (unsigned char *)malloc(FileSize * sizeof(char)) ;
    
    for (i = 0; i < FileSize; ++i)
    {
        fread(&p[i], sizeof(char), 1 , fpin) ;
    }

    //write to file
    fpout = fopen("2.txt", "wt") ;
    
    for (i = 0; i < FileSize; ++i)
    {
        fprintf(fpout, " %02X", p[i]) ;
    }
    
    free(p) ;
    fclose(fpout) ;
    fclose(fpin) ;
    return 0 ;
}

2。把文本恢複到二進位檔案

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fpin, *fpout;
    unsigned char c, c0, c1 ;
    if ((fpin = fopen("2.txt", "rt")) == NULL)
    {
        printf("file can not open!\n") ;
        exit(0) ;
    }
    
    if ((fpout = fopen("new.rar", "wb")) == NULL)
    {
        printf("file can not open!\n") ;
        exit(0) ;
    }

    while (!feof(fpin))
    {
        if((c0 = fgetc(fpin) - '0') >= 10)
            c0 -= 7 ;  /**//*7='A'-'0'-10*/
        
        if(feof(fpin)) 
            break ;     /**//*檔案尾前還有個'\0'字串結束標誌*/
        
        if((c1 = fgetc(fpin) - '0') >= 10)
            c1 -= 7 ;
        
        c = (c0 << 4) + c1 ;
        
        fwrite(&c, 1, 1, fpout) ;      /**//*也可以用fprintf(fpout, "%c", c) ;*/
    }

    fclose(fpin) ;
    fclose(fpout) ;
    return 0 ;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.