文件二進制的讀寫

讀文件並以二進制形勢轉到另外一新建文件中html

ExpandedBlockStart.gif ContractedBlock.gif /**/ /****************************************
InBlock.gif*edward nic
InBlock.gif*2007.04.25
InBlock.gif*
ExpandedBlockEnd.gif***************************************
*/

None.gif#include 
< stdio.h >
None.gif#include 
< stdlib.h >
None.gif#include 
< sys\stat.h >    // for fstst()
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
struct stat statBuffer ;
InBlock.gif    FILE        
*fpin , *fpout;
InBlock.gif    
char        *p ;
InBlock.gif    
int         FileSize ;
InBlock.gif
InBlock.gif    fpin 
= fopen("1.rar""rb") ;
InBlock.gif    fstat(fileno(fpin), 
&statBuffer) ;
InBlock.gif    FileSize 
= statBuffer.st_size ;
InBlock.gif    printf(
"%d\t", statBuffer.st_size) ;
InBlock.gif    
InBlock.gif    p 
= (char *)malloc(FileSize * sizeof(char)) ;
InBlock.gif
InBlock.gif    fread(p, FileSize, 
1 , fpin) ;
InBlock.gif
InBlock.gif    fpout 
= fopen("2.rar""wb") ;
InBlock.gif    fwrite(p, FileSize, 
1 , fpout) ;
InBlock.gif
InBlock.gif    free(p) ;
InBlock.gif    fclose(fpout) ;
InBlock.gif    fclose(fpin) ;
InBlock.gif    
return 0 ;
ExpandedBlockEnd.gif}




讀相似「\xff\xaa\x02…」文本文件並還原到二進制文件
1。把二進制文件轉\xff\xaa\x02…」文本型式
None.gif // 須要 unsigned char
None.gif

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

InBlock.gif
InBlock.gif    
//write to file
InBlock.gif
    fpout = fopen("2.txt""wt") ;
InBlock.gif    
InBlock.gif    
for (i = 0; i < FileSize; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        fprintf(fpout, 
" %02X", p[i]) ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    free(p) ;
InBlock.gif    fclose(fpout) ;
InBlock.gif    fclose(fpin) ;
InBlock.gif    
return 0 ;
ExpandedBlockEnd.gif}



2。把文本恢復到二進制文件

None.gif #include  < stdio.h >
None.gif#include 
< stdlib.h >
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    FILE 
*fpin, *fpout;
InBlock.gif    unsigned 
char c, c0, c1 ;
InBlock.gif    
if ((fpin = fopen("2.txt""rt")) == NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        printf(
"file can not open!\n") ;
InBlock.gif        exit(
0) ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
if ((fpout = fopen("new.rar""wb")) == NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        printf(
"file can not open!\n") ;
InBlock.gif        exit(
0) ;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
while (!feof(fpin))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if((c0 = fgetc(fpin) - '0'>= 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif            c0 
-= 7 ;  /**//*7='A'-'0'-10*/
InBlock.gif        
InBlock.gif        
if(feof(fpin)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
break ;     /**//*文件尾前還有個'\0'字符串結束標誌*/
InBlock.gif        
InBlock.gif        
if((c1 = fgetc(fpin) - '0'>= 10)
InBlock.gif            c1 
-= 7 ;
InBlock.gif        
InBlock.gif        c 
= (c0 << 4+ c1 ;
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        fwrite(
&c, 11, fpout) ;      /**//*也能夠用fprintf(fpout, "%c", c) ;*/
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    fclose(fpin) ;
InBlock.gif    fclose(fpout) ;
InBlock.gif    
return 0 ;
ExpandedBlockEnd.gif}

None.gif

轉載於:https://www.cnblogs.com/nniixl/archive/2007/04/25/726352.htmlpost