当前位置:网站首页>C language partition bin file program
C language partition bin file program
2022-06-12 15:26:00 【Jamse_ Orz】
/**
******************************************************************************
* @file main.c
* @author Earlybird
* @version V1.0.0
* @date 30-May-2022
* @brief Division bin File is a file of specified size
******************************************************************************
* @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)
{
/* Read bin Document type verification */
if (!strstr(file_addr, ".bin"))
{
printf("file type incorrect, please input proper bin file.");
return RUNERROR;
}
/* Require chunk size verification */
if (!file_cut_size)
{
printf("file cut size incorrect, please input the size greater than 0.");
return RUNERROR;
}
/* Read 2 Base number bin file */
FILE* fp_bin = fopen(file_addr, "rb");
if (!fp_bin)
{
perror("fopen bin file error");
return RUNERROR;
}
/* Calculation bin Total file size */
int file_all_size = 0;
fseek(fp_bin, 0, SEEK_END);
file_all_size = ftell(fp_bin);
/* Calculate the number of files after splitting */
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);
/* Generate new file after split */
fseek(fp_bin, 0, SEEK_SET);
char* file_fifo = (char*)malloc(sizeof(char) * file_cut_size); // Define split file buffer
char* file_cut_addr = (char*)malloc(strlen(file_addr) - 4 + 4 + 10); // Define the storage address of the split file
char* file_name = (char*)malloc(strlen(file_addr));
strncpy(file_name, file_addr, strlen(file_addr) - 4); // Read the storage address and name of the source file
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); // Generate split file storage address
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); // The number of bytes written is the return value read
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;
}

边栏推荐
- Change according to the situation, the road to promotion in the second half of 2022
- ssm中的文件上传和下载
- ROS初学者编写小乌龟以一定速度旋转一定角度的server
- Array related content
- Self induction of exception handling
- Idea pull branch code
- #include使用“文件名“和<文件名>引入头文件的区别及简述
- 如何写年终总结
- FIRSTVT and LASTVT vernacular
- [jvm learning] virtual machine stack
猜你喜欢
![[jvm learning] class loading subsystem](/img/60/e863495ce4ea5826d1404a73c90033.jpg)
[jvm learning] class loading subsystem

Mh32f103arpt6 hardware and software compatible alternative to stm32f103rct6

Increase the maximum number of MySQL connections

Solution of user and root forgetting password in virtual machine

Function recursion example

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

解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击

Simple crawler framework: parsing 51job page position information

任务 输出密雪冰城主题曲 0612

Microservice fault tolerance
随机推荐
Some useful websites
如何使用Grafana轻松实现OVL数据可视化
Dart typedef的理解
h3c GR5200路由器上如何设置公网ip可以访问
虚拟机中用户和root忘记密码解决办法
Deepin20.6 RTX3080 安装显卡驱动510.60.02、CUDA11.6、PyTorch1.11
IMU的学习记录
[jvm learning] virtual machine stack
安装rhel 7/8 (红帽)虚拟机(转载)
分布式并发重复提交问题
Seaborn Brief
CUDA out of memory or brokenpipeerror: [errno 32] broken pipe or oserror: [winerror 1455] solution to the problem that the page file is too small
Apprendre est une chose contre la nature humaine
Village to village communication (and collective search)
广播和多播(TCP/IP详解卷1/2)
Understanding of Odom coordinate system
odom坐标系的理解
Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
How to write year-end summary
5G新方案!升级现有的基站和UE模拟器至5G毫米波频段