C之bin_cut

// test.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

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

#define SIZE    16
#define LAST_SECTOR_FLAG    0x000BFFF0

int main(int argc, char *argv[])
{
    FILE *fp_in = NULL;
    FILE *fp_out = NULL;
    unsigned char dat[SIZE];
    unsigned char buf[SIZE];
    unsigned long cnt;
    unsigned long flag;
    unsigned long dummy;

    char path_buffer[_MAX_PATH];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char filename[_MAX_FNAME];
    char ext[_MAX_EXT];
    char file_to_write[_MAX_FNAME + _MAX_EXT];

    if (NULL != argv[1]) {
        fp_in = fopen(argv[1], "rb");
    }

    cnt = flag = dummy = 0;

    if (NULL == fp_in) {
        printf("No such file!\n");
    } else {
        fread(buf, 1, SIZE, fp_in);
        while (!feof(fp_in)) {
            memset(dat, 0xFF, SIZE);
            if (0 == memcmp(dat, buf, SIZE)) {
                dummy ++;
            } else {
                if (cnt != LAST_SECTOR_FLAG >> 4) {
                    flag = cnt;
                    //printf("0x%X\n", flag << 4);
                }
            }
            cnt ++;
            fread(buf, 1, SIZE, fp_in);
        }

        fclose(fp_in);
    }

    printf("0x%08X\n", dummy << 4);

    if (NULL != argv[1]) {
        fp_in = fopen(argv[1], "rb");
    }

    cnt = 0;

    if (NULL == fp_in) {
        printf("No such file!\n");
    } else {
        _splitpath(argv[1], drive, dir, filename, ext);

        strcpy(file_to_write, filename);
        strcat(file_to_write, ".cut.bin");

        fp_out = fopen(file_to_write, "wb");
        if (NULL == fp_out) {
            printf("No such file!\n");
        }

        fread(buf, 1, SIZE, fp_in);
        while (!feof(fp_in)) {
            if (cnt <= flag) {
                memcpy(dat, buf, SIZE);

                fwrite(dat, 1, SIZE, fp_out);
                fread(buf, 1, SIZE, fp_in);
            } else {
                break;
            }
            cnt ++;
        }

        fclose(fp_in);
        fclose(fp_out);
    }

    Sleep(1000);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值