C之bin_cut

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011958166/article/details/81538038
// 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;
}

猜你喜欢

转载自blog.csdn.net/u011958166/article/details/81538038
cut