本帖最后由 三点六万千米 于 2022-11-11 18:02 编辑
合并txt文件
使用终端运行:
结果合并结果保存到第一个文件中:
代码如下:
[Asm] 纯文本查看 复制代码 #include <stdio.h>
#include <stdlib.h>
unsigned char *buffer;
void merge(char target[], char source[]);
int main(int argc, char* argv[])
{
int i;
if (argc <= 2)
{
printf("Usage:merge file1 file2 file3\n");
exit(1);
}
buffer = (unsigned char*)malloc(80);
for (i = 2; i < argc; i++)
{
merge(argv[1], argv[i]);
}
return 0;
}
void merge(char target[], char source[])
{
FILE* fp1, * fp2;
if ((fp1 = fopen(target, "a")) == NULL)
{
printf("文件%s打开失败\n", target);
exit(1);
}
if ((fp2 = fopen(source, "r")) == NULL)
{
printf("文件%s打开失败\n", source);
exit(1);
}
fputs("\n", fp1);
fputs("Filename:", fp1);
fputs(source, fp1);
fputs("\n----------------------------\n", fp1);
while (fgets(buffer, 80, fp2))
fputs(buffer, fp1);
fclose(fp1);
fclose(fp2);
}
|