[C语言] 文件操作,对二进制文件追加二进制数据与补零的两个函数;

珍藏...

#define BUFFER_SIZE 1024

//在尾部补零
void appendZeros(FILE *fp, int size){
    FILE *fd;
    unsigned int buffer[size];
    strncpy(buffer,"",sizeof(buffer));
    if ( ( fd=fopen(fp,"a+") ) == NULL ){
        printf ( "在补零时,打开目标文件失败!\n" );
        exit(1);
    }

    if ( size != 0 ){
        fwrite(buffer, 1 , size , fd);
//        printf ( "补零数目:\t%d\n", size);
    }
    fclose(fd);
}

//在尾部追加二进制文件
void appendFile(FILE *fp, FILE *body) { 
    int n=0;
    FILE *in,*out;
    in = fp;
    out = body;
    u8 buffer[BUFFER_SIZE];
    if ( (in = fopen(fp, "rb")) == NULL){
        printf ( "打开来源文件失败!\n" );
        exit(1);
    }
    if ( (out=fopen( out, "a+")) == NULL ){
        printf ( "打开目标文件失败!\n" );
        exit(1);
    }
    while (!feof(in)) {
        n = fread(buffer, 1, BUFFER_SIZE, in);
        fwrite(buffer, 1, n, out);
    }
    fclose(in);
    fclose(out);
}

 

posted @ 2013-03-27 00:57  scue  阅读(803)  评论(0编辑  收藏  举报