文件拆分器(FileSplitter)

FileSplitter是一个轻量级的文件拆分程序,支持C++和C语言,可在GitHub上获取包含README、源码和可执行文件的存储库。此外,你也可以在CSDN找到完全免费的资源,包括源码和适用于Windows的可执行文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件拆分器

GitHub存储库(README、源码及可执行文件)

FileSplitter

CSDN资源(完全免费, 0 0 0 C币)

源码
可执行文件(Windows

源码

跨平台创建文件夹 cp_dir.h
#ifndef CP_DIR_H
#define CP_DIR_H

/* C standard library */
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/* Platform library */
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#elif _LINUX
#include <stdarg.h>
#include <sys/stat.h>
#endif

/* Function for each platform */
#ifdef _WIN32
#define ACCESS _access
#define MKDIR(a) _mkdir((a))
#elif _LINUX
#define ACCESS access
#define MKDIR(a) mkdir((a), 0755)
#endif

/* Cross-platform function to create a directory */
int32_t create_dir(const char *s_path)
{
	int32_t i = 0, ret, sz_len = strlen(s_path);
	char *s = (char *)calloc(sz_len + 3, sizeof(char));

	if (!s)
	{
		return -1;
	}
	strcpy(s, s_path);
	// append '/'
	if (s[sz_len - 1] != '\\' && s[sz_len - 1] != '/')
	{
		s[sz_len++] = '/';
		s[sz_len++] = '\0';
	}

	// make directory
	for (i = 0; i < sz_len; i++)
	{
		if (s[i] == '\\' || s[i] == '/')
		{
			s[i] = '\0';

			// create if not exist
			ret = ACCESS(s, 0);
			if (ret != 0)
			{
				ret = MKDIR(s);
				if (ret)
				{
					return -1;
				}
			}
			// replace '\\' with '/'
			s[i] = '/';
		}
	}

	return 0;
}

#undef ACCESS
#undef MKDIR

#endif
程序主体 FileSplitter.cpp
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cstring>
#include "cp_dir.h"

using byte_t = char;

inline int split(std::size_t mx_fsz, std::string path)
{
	std::ifstream in = std::ifstream(path, std::ios::in | std::ios::binary);
	byte_t *buf = new byte_t[mx_fsz + 1]{'\0'};
	std::size_t cnt = 0;
	std::ofstream out;
	if (!buf)
	{
		std::cerr << "Fatal error: Failed to allocate memory for the buffer!" << std::endl;
		goto ERR_RET;
	}
	if (!in)
	{
		std::cerr << "Failed to open the input file: " << path << std::endl;
		goto FAIL_RET;
	}
	if (create_dir((path += "-split_res/").c_str()))
	{
		std::cerr << "Failed to create the output directory: "
				  << path << std::endl;
		goto FAIL_RET;
	}
	while (in)
	{
		in.read(buf, mx_fsz);
		out.open(path + std::to_string(cnt) + ".out",
				 std::ios::out | std::ios::trunc | std::ios::binary);
		if (!out)
		{
			std::cerr << "Failed to open the output file: "
					  << path + std::to_string(cnt) + ".out"
					  << std::endl;
			goto FAIL_RET;
		}
		out.write(buf, (in ? mx_fsz : std::strlen(buf)));
		out.close();
		std::fill(buf, buf + mx_fsz + 1, '\0'); // clear buffer
		++cnt;
	}
	in.close();
	delete[] buf;
	return 1;
FAIL_RET:
	delete[] buf;
	return 0;
ERR_RET:
	return -1;
}

int main(int argc, char *argv[])
{
	std::ios::sync_with_stdio(false);
	std::size_t mx_fsz = 0;
	std::string path;
	std::stringstream ss;
	if (argc > 2)
	{
		for (int i = 1; i + 1 < argc; i += 2)
		{
			ss.clear();
			ss << argv[i];
			if (!(ss >> mx_fsz))
			{
				std::cerr << "Invalid parameter for maximal file size!" << std::endl;
				continue;
			}
			if (split(mx_fsz, path = argv[i + 1]) == -1)
			{
				goto ERR_RET;
			}
		}
	}
	else
	{
		while (std::cin)
		{
			std::cout << "Input the maximal size of each output file: ";
			if (!(std::cin >> mx_fsz))
			{
				if (std::cin.eof())
				{
					std::cout << "Exit." << std::endl;
					goto RET;
				}
				std::cerr << "Invalid input for maximal file size!" << std::endl;
				goto RET;
			}
			std::getline(std::cin, path); // blank
			std::cout << "Input the path of the file to split: ";
			if (!std::getline(std::cin, path))
			{
				if (std::cin.eof())
				{
					std::cout << "Exit." << std::endl;
					goto RET;
				}
				std::cerr << "Fatal error: Failed to write data into the string!" << std::endl;
				goto ERR_RET;
			}
			if (split(mx_fsz, path) == -1)
			{
				goto ERR_RET;
			}
		}
	}
RET:
	return 0;
ERR_RET:
	return -1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值