【发布时间】:2014-09-12 03:38:05
【问题描述】:
我正在尝试找不到可以合并两个文件的程序,任何文件,如 .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 时,这怎么可能?
提前感谢您的帮助
【问题讨论】:
-
那么你到底想做什么呢?请给出两个文件的示例输入,每个文件大约 5 行,以及您期望的输出。
-
假设这是在 .nc 文件中读取的 1.5GB 长的数据
-
'4344 4601 0000 0000 0000 000A 0000 0004 0000 0003 6c6f 6e00 0000 0090 0000 0003 6c61 7400 0000 0049 0000 0004 7469 6d65 0000 05b4 0000 0005 6e62 6e64 7300 0000 0000 0002 0000 000C 0000 0007 0000 000B'
-
显示的代码通过交替合并文本行来工作,并且行缓冲区大小为 200 个字符。你只提到了一个文件而不是它的格式,但是除非你有两个 text 文件作为输入,并且它们的行都没有超过 199 个字符(包括换行符),否则它不会起作用。
-
如果您的数据不是文本,您不能指望
fgets和fputs有用。您的数据中显然包含空字节。 (即,它不是文本)