大文件分割C语言实现

主程序

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define BYTE_PER_FILE 1048576 // 定义每个子文件的字节数

int main()
{
    FILE *f = fopen("video.mp4", "rb"); // 打开源文件
    FILE *output_file = NULL;
    char buffer;
    long file_cnt = 0;
    long byte_cnt = BYTE_PER_FILE;

    if (f)
    {
        while (fread(&buffer, 1, 1, f) == 1) // 读取源文件中的所有字节
        {
            if (byte_cnt == BYTE_PER_FILE) // 字节计数器满时切换下一个子文件
            {
                byte_cnt = 0;
                file_cnt++;
                fclose(output_file);
                char f_num[100];
                char f_path[100] = "./video/split_";
                sprintf(f_num, "%d", file_cnt);
                strcat(f_path, f_num);
                strcat(f_path, ".data");
                output_file = fopen(f_path, "wb");
            }
            // printf("%c ",buffer); 
            fwrite(&buffer,1,1,output_file);
            byte_cnt++;
        }

        // 关闭文件
        fclose(f);
    }
    else
    {
        printf("文件打开出错\n");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值