⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 splicer.c

📁 用C语言写的文件合并
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_SIZE 1024
#define FILE_NAME_LEN 120

void append(FILE *dest, FILE *source);

int main(void)
{
	FILE *f_src, *f_dest;
	
	int i;
	int files = 0;
	char file_dest[FILE_NAME_LEN] = {"image.bin"};
	char file_src[4][20] = {"UB_RAM.bin", "UB_ROM_dev.bin", "rfs_jffs2.img", "uImage_dev"};

	char (*pf)[20];
	pf = file_src;

//	puts("Please enter name of destination file: ");
//	gets(file_dest);

	if ((f_dest = fopen(file_dest, "ab")) == NULL)
	{
		fprintf(stderr, "Can't open the destination file %s.\n", file_dest);
		exit(1);
	}

	if (setvbuf(f_dest, NULL, _IOFBF, BUF_SIZE) != 0)
	{
		fputs("Can't create output buffer.\n", stderr);
		exit(2);
	}

//	puts("Pleade enter name of first file (empty line to quit): ");

//	while(gets(file_src) && file_src[0] != '\0')
//	{
	for(i = 0; i < 4; i++)
	{
		if (strcmp(*pf, file_dest) == 0)
		{
			fputs("Can't append file to itself.\n", stderr);
		}

		else if ((f_src = fopen(*pf, "rb")) == NULL)
		{
			fprintf(stderr, "Can't open file %S.\n", pf);
		}

		else 
		{
			if (setvbuf(f_src, NULL, _IOFBF, BUF_SIZE) != 0)
			{
				fputs("Can't create input buffer.\n", stderr);
				continue;
			}

			append(f_dest, f_src);

			if (ferror(f_src) != 0)
			{
				fprintf(stderr, "Error in reading file %s.\n", pf);
			}

			if (ferror(f_dest) != 0)
			{
				fprintf(stderr, "Error in writing file %s.\n", file_dest);
			}

			fclose(f_src);

			files++;
			pf++;

			printf("File %s appended successfully.\n", pf);

//			puts("Please enter the name of the next file (empty line to quit): ");

		}

	}

	printf("Done. %d files appended successfully.\n", files);

	fclose(f_dest);

	return 0;

}

void append(FILE *dest, FILE *source)
{
	size_t bytes;
	static char temp[BUF_SIZE];

	while ((bytes = fread(temp, sizeof(char),BUF_SIZE, source))>0)
	{
		fwrite(temp, sizeof(char), bytes, dest);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -