C/C++实现文本文件分片操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS

void splitTextFile(const char* file_path, int max_embed, int first_slice_length, int second_slice_length) {
    
    FILE* input_file = fopen(file_path, "r");
    if (input_file == NULL) {
        printf("Error: Cannot open input file.\n");
        return;
    }
    fseek(input_file, 0, SEEK_END);
    long file_size = ftell(input_file);
    rewind(input_file);

    FILE* first_slice_file = fopen("first_slice.bin", "w");
    FILE* second_slice_file = fopen("second_slice.bin", "w");

    if (first_slice_file == NULL || second_slice_file == NULL) {
        printf("Error: Cannot create output files.\n");
        fclose(input_file);
        return;
    }

    char *buffer = new char[file_size+1];
    fread(buffer, 1, file_size, input_file);
    fclose(input_file);
    buffer[file_size] = '\0';


    char* info_first = new char[first_slice_length + 1];

    for (int i = 0; i < first_slice_length; i++)
    {
        info_first[i] = buffer[i];
    }
    info_first[first_slice_length] = '\0';
    fputs(info_first, first_slice_file);
    fclose(first_slice_file);


    char* info_second = new char[second_slice_length + 1];
    int end = first_slice_length + second_slice_length;
    int j = 0;
    for (int i = first_slice_length; i < end; i++)
    {
        info_second[j] = buffer[i];
        j++;
    }
    info_second[second_slice_length] = '\0';
    fputs(info_second, second_slice_file);
    fclose(second_slice_file);



    int remain = file_size - first_slice_length - second_slice_length;
    int slice = remain / max_embed;

    char* remain_buffer = new char[remain + 1];
    int m = 0;
    for (int k = end; k < file_size; k++)
    {
        remain_buffer[m] = buffer[k];
        m++;
    }

    for (int i = 0; i < slice; i++)
    {
        const char* file_base = "remain_slice";
        char segment_file_name[100];
        snprintf(segment_file_name, sizeof(segment_file_name), "%s_%d.bin", file_base, i+1);

        FILE* output_file = fopen(segment_file_name, "w");
        fwrite(remain_buffer + i * max_embed, 1, max_embed, output_file);
        fclose(output_file);
    }

    const char* output = "remain_slice_final.bin";
    FILE* outputfinal_file = fopen(output, "w");
    fwrite(remain_buffer + slice * max_embed, 1, remain - slice * max_embed, outputfinal_file);

    fclose(outputfinal_file);
    delete[] buffer;
    buffer = NULL;
}

int main() {
    const char* file_path = "data.bin";
    int max_embed = 5;
    int first_slice_length = 16;
    int second_slice_length = 16;

    splitTextFile(file_path, max_embed, first_slice_length, second_slice_length);

    printf("Text file has been split into slices.\n");

    return 0;
}
接口函数 void split(const char* infilepath, int max_embed, int first_slice_length, int second_slice_length);
infilepath:需要分片的文本路径
max_embed:最大嵌入量
first_slice_length:第一片长度

second_slice_length:第二片长度

本函数实现的功能 : 先将infilepath的.bin文件按first_slice_length和second_slice_length分为两片,然后剩下的按max_embed平均分片。

第一片:first_slice.bin

第二片:second_slice.bin

剩余:remain_slice_k.bin k=1,2...

最后一片:remain_slice_final.bin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值