请教高手如何改变c语言中打开二进制文件的个数
deane 2000-05-01 12:59:00 近日用c作了一个程序,是将多个wav文件合成,基本上已经成功,
但昨天调试时发现连接10个以上文件时发生错误,
具体是当打开第十一个二进制文件时,c说" ",后来我试了多种方
法都不好使,其中包括将前几个文件用完后关闭,再fopen其他的文件;
和设两个函数,每个打开5个文件,用完后关闭.我现在怀疑是系统设定
了一个打开二进制文件的默认值,但不知怎样修改,望各位高手指点
附源代码:
思路:
将10个文件连接成一个wav文件,首先将第一个文件靠到
第十一个文件,用到filecopy函数,而后依次将第二,第三......连接
到第十一个文件,连接用到filelink函数.第十一为最终文件.
其中filelink涉及到wav文件头格式.本函数在连接10个以下文件时好使.
#include <stdio.h>
char buff[512];
FILE *f1,*f2,*f3,*f4,*f5,*f6,*f7,*f8,*f9,*f10,*f11,*f12;
long s1,s2,s3,s4;
main()
{
void filecopy(FILE *f,FILE *nf);
void filelink (FILE *f,FILE *nf);
//打开文件
if ((f1=fopen("gcdz01.wav","rb"))==0)
{printf("filecopy f1 failure");exit(0);}
if ((f2=fopen("gcdz02.wav","rb"))==0)
{printf("flink f2 failure");exit(0);}
if ((f3=fopen("gcdz03.wav","rb"))==0)
{printf("flink f3 failure");exit(0);}
if ((f4=fopen("gcdz04.wav","rb"))==0)
{printf("flink f4 failure");exit(0);}
if ((f5=fopen("gcdz05.wav","rb"))==0)
{printf("f5"); }
if ((f6=fopen("gcdz06.wav","rb"))==0)
{printf("f6"); }
if ((f7=fopen("gcdz07.wav","rb"))==0)
{printf("f7"); }
if ((f8=fopen("gcdz08.wav","rb"))==0)
{printf("f8"); }
if ((f11=fopen("11.wav","wb+"))==0)
{printf("f11");}
filecopy(f1,f11);
filelink(f2,f11);
filelink(f3,f11);
filelink(f4,f11);
filelink(f5,f11);
filelink(f6,f11);
filelink(f7,f11);
filelink(f8,f11);
fclose (f1);fclose(f2);fclose (f3);fclose (f4);fclose(f5);fclose(f6);fclose(f8);fclose(f7);
if ((f10=fopen("gcdz10.wav","rb"))==0)
{printf("f10"); }
if ((f9=fopen("gcdz09.wav","rb"))=0)
{printf("f9"); }
filelink(f9,f11);
filelink(f10,f11);
fclose(f11);
fclose(f9);fclose(f10);
}
//copy file拷贝函数
void filecopy(FILE *f,FILE *nf)
{
int n=0;
char ch;
rewind (f);rewind (nf);
while (fread (buff,512,1,f))
{ fwrite (buff,512,1,nf);
n++;
}
fseek (f,n*512L,0);
while (!feof(f))
{ ch=fgetc(f); fputc(ch,nf);
}
}
//file link连接函数
void filelink(FILE *f,FILE *nf)
{
int n=0; char ch;
//get length
fseek(nf,54L,0);fread(&s1,4,1,nf);fseek(f,54L,0);fread(&s2,4,1,f);
s4=s1+s2-1;s3=s4+50;
//write length
fseek (nf,4L,0);fwrite (&s3,4,1,nf);fseek(nf,54L,0);fwrite(&s4,4,1,nf);
//link files
fseek (nf,-1L,2);fseek (f,58L,0);
n=0;
while (fread(buff,512,1,f))
{
fwrite (buff,512,1,nf);
n++;
}
fseek(f,512L*n,0);
while (!feof(f))
{
ch=fgetc(f); fputc(ch,nf);
}
}