|
-
December 8th, 2004, 10:16 AM
#1
Help,I have trouble in writing a file into a buffer and anoter file!
There is a MIDI file "hello.mid"(Size : 1M).
I want to write the data of it to a buffer and anther file.
Code:
#include "stdafx.h"
#include "stdio.h"
using namespace std;
const int nSize = 2*1024;//Max size :2M
int _tmain(int argc, _TCHAR* argv[])
{
FILE* pf, * pf1;
int nLength = 0;
unsigned char * pBuffer = new unsigned char[nSize];
char* strPathName = "hello.mid";
if((pf = fopen(strPathName,"r"))==NULL)
{
cout<<"Can't open file"<<endl;
system("pause");
return 0;
}
//Write data into *pBuffer
while(!feof(pf))
{
fread(pBuffer,sizeof(unsigned char),1,pf);
nLength++;
}
//Write data into anther file named "temp.mid":
if((pf1 = fopen("temp.mid","w+"))==NULL)
{
cout<<"Can't write file"<<endl;
system("pause");
return 0;
}
unsigned char ch=fgetc(pf);
while(!feof(pf))
{
fputc(ch,pf1);
ch=fgetc(pf);
nLength++;
}
fclose(pf1);
fclose(pf);
system("pause");
return 0;
}
Problem:
1." Temp.mid"is lager than "hello.mid",in other words, "temp.mid" has
indescribable redundant data, why?
2. Why can't "temp.mid" be generated in this way :
fwrite(pBuffer,sizeof(unsigned char),nLength,pf1); ?
Thanks!
Last edited by ArmStronger; December 8th, 2004 at 11:13 AM.
Communication of thoughts makes one strong.
-
December 8th, 2004, 10:25 AM
#2
Re: Help,I have trouble in writing a file into a buffer and anoter file!
Midi are binary files and you're opening it as text. Read and write it binary.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|