gpt4 book ai didi

c - 合并C中的两个文件,任何文件

转载 作者:太空宇宙 更新时间:2023-11-04 07:15:41 24 4
gpt4 key购买 nike

我正在尝试找到一个可以合并两个文件的程序,任何文件,如 .nc 文件

我需要复制一个 1.5GB 长的 .nc 文件,但我不想将其作为文本打开并复制并粘贴其内容以使其更大

我在网上找到这段代码,但它不适用于像我这样的大文件?

它适用于我注意到的小的简单文本文件

#include<stdio.h>
main()
{
char f1[10],f2[10];
puts("enter the name of file 1"); /*getting the names of file to be concatenated*/
scanf("%s",f1);
puts("enter the name of file 2");
scanf("%s",f2);
FILE *fa,*fb,*fc;
fa=fopen(f1,"r"); /*opening the files in read only mode*/
fb=fopen(f2,"r");
fc=fopen("merge.txt","w+"); /*opening a new file in write,update mode*/
char str1[200];
char ch1,ch2;
int n=0,w=0;
while( (( ch1=fgetc(fa) )!=EOF)&&((ch2=fgetc(fb))!=EOF))
{
if(ch1!=EOF) /*getting lines in alternately from two files*/
{
ungetc(ch1,fa);
fgets(str1,199,fa);
fputs(str1,fc);
if(str1[0]!='\n') n++; /*counting no. of lines*/
}
if(ch2!=EOF)
{
ungetc(ch2,fb);
fgets(str1,199,fb);
fputs(str1,fc);
if(str1[0]!='\n')n++; /*counting no.of lines*/
}
}
rewind(fc);
while((ch1=fgetc(fc))!=EOF) /*countig no.of words*/
{
ungetc(ch1,fc);
fscanf(fc,"%s",str1);
if(str1[0]!=' '||str1[0]!='\n')
w++;
}
fprintf(fc,"\n\n number of lines = %d \n number of words is = %d\n",n,w-1);
/*appendig comments in the concatenated file*/
fclose(fa);
fclose(fb);
fclose(fc);
}

我正在合并同一个文件,以便复制它,但是当它吐出文件的结果时,它说文件只有 8,668 字节?!?当原始文件为 1.5GB 时,这怎么可能?

提前感谢您的帮助

最佳答案

您不需要编写程序来执行此操作,一个已经存在。

cat foo.nc bar.nc > foobar.nc

bar.nc 将连接到 foobar.nc 中的 foo.nc 的末尾。这适用于二进制和文本数据。如果要添加到现有文件:

cat foo.nc >> bar.nc

foo.nc 将添加到 bar.nc 的末尾。

有关详细信息,请参阅 man cat

关于c - 合并C中的两个文件,任何文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com