当前位置:网站首页>C语言 分割bin文件程序
C语言 分割bin文件程序
2022-06-12 15:19:00 【Jamse_Orz】
/**
******************************************************************************
* @file main.c
* @author Earlybird
* @version V1.0.0
* @date 30-May-2022
* @brief 分割bin文件为指定大小文件
******************************************************************************
* @attention
*
* Copyright (c) 2022 INESA(Group)Co., Ltd. R&D Center.
* All rights reserved.
*
******************************************************************************
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
#define RUNERROR -1
#define RUNSUCESS 0
int cut_bin_file(char* file_addr, int file_cut_size)
{
/* 读取bin文件类型校验 */
if (!strstr(file_addr, ".bin"))
{
printf("file type incorrect, please input proper bin file.");
return RUNERROR;
}
/* 要求分割块大小校验 */
if (!file_cut_size)
{
printf("file cut size incorrect, please input the size greater than 0.");
return RUNERROR;
}
/* 读取2进制bin文件 */
FILE* fp_bin = fopen(file_addr, "rb");
if (!fp_bin)
{
perror("fopen bin file error");
return RUNERROR;
}
/* 计算bin文件总大小 */
int file_all_size = 0;
fseek(fp_bin, 0, SEEK_END);
file_all_size = ftell(fp_bin);
/* 计算分割后文件个数 */
int file_cut_num = 0;
file_cut_num = (file_all_size % file_cut_size) ? (file_all_size / file_cut_size) + 1 : (file_all_size / file_cut_size);
printf("bin file size: %dB, cut file size: %dB, cut file num: %d.\n", file_all_size, file_cut_size, file_cut_num);
/* 生成分割后新文件 */
fseek(fp_bin, 0, SEEK_SET);
char* file_fifo = (char*)malloc(sizeof(char) * file_cut_size); // 定义分割文件缓冲区
char* file_cut_addr = (char*)malloc(strlen(file_addr) - 4 + 4 + 10); //定义分割文件存放地址
char* file_name = (char*)malloc(strlen(file_addr));
strncpy(file_name, file_addr, strlen(file_addr) - 4); // 读取源文件存放地址及名称
file_name[strlen(file_addr) - 4] = 0;
int file_cut_cnt = 0;
size_t file_read_size = 0;
while (1)
{
memset(file_fifo, 0, file_cut_size);
file_read_size = fread(file_fifo, 1, file_cut_size, fp_bin);
if (!file_read_size)
{
printf("cut bin file done.\n");
break;
}
file_cut_cnt++;
sprintf(file_cut_addr, "%s_%003d.bin", file_name, file_cut_cnt); // 生成分割文件存放地址
FILE* fp_cut_bin = fopen(file_cut_addr, "wb");
if (!fp_cut_bin)
{
perror("create cut file error");
return RUNERROR;
}
fwrite(file_fifo, 1, file_read_size, fp_cut_bin); //写入字节数是读出来的返回值
printf("%s, size: %zdB, create done.\n", file_cut_addr, file_read_size);
fclose(fp_cut_bin);
}
fclose(fp_bin);
free(file_fifo);
file_fifo = NULL;
free(file_cut_addr);
file_cut_addr = NULL;
free(file_name);
file_name = NULL;
return RUNSUCESS;
}
int main(int argc, char** argv)
{
if (argc == 3)
{
cut_bin_file(argv[1], atoi(argv[2]));
}
else
{
printf("command format: cutbin.exe [file_name.bin] [cut_size]\n");
}
system("pause");
return EXIT_SUCCESS;
}

边栏推荐
- ssm中的文件上传和下载
- Design concept of ORM framework
- Introduction to microservices
- Deepin20.6 RTX3080 安装显卡驱动510.60.02、CUDA11.6、PyTorch1.11
- C escape character
- C string
- Solutions to some problems of scuacm22 retreat competition before summer training
- PTA:自测-3 数组元素循环右移问题 (20分)
- Microservice fault tolerance
- Introduction to Eureka
猜你喜欢

Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol

UDP总结(TCP/IP详解卷1/2)

Function recursion example

Servlet connects to database to realize user login function

机器人前行、旋转的service编写
![[jvm learning] types of GC and allocation process of objects on JVM heap](/img/f3/8141be7bcbf79d9c874f9cc09683c8.jpg)
[jvm learning] types of GC and allocation process of objects on JVM heap

Scala下载及IDEA安装Scala插件(保姆级教程超详细)

ROS初学者编写小乌龟以一定速度旋转一定角度的server

Acwing暑期每日一题(6月10日性感素数)

增加mysql的最大连接数
随机推荐
How to set public IP access on the H3C gr5200 router
Xshell 7 official website free download
Village to village communication (and collective search)
Design concept of ORM framework
New features of ES6
Use and understanding of generics
学习是一件逆人性的事情(成为高手的内功心法)
Solutions to some problems of scuacm22 retreat competition before summer training
MH32F103ARPT6软硬件兼容替代STM32F103RCT6
Distributed concurrent repeated submission
Singleton mode instance
SOA Architecture
Use of boost:: bind() in ROS
增加mysql的最大连接数
远程操控其它电脑--详细教程
Industrial end: a new battlefield of 618
Servlet connects to database to realize user login function
Kinect2.0+ORBSLAM2_with_pointcloud_map
Wild pointer understanding
Deepin20.6 RTX3080 安裝顯卡驅動510.60.02、CUDA11.6、PyTorch1.11