ArmStronger
December 8th, 2004, 09:16 AM
There is a MIDI file "hello.mid"(Size : 1M).
I want to write the data of it to a buffer and anther file.
#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!
I want to write the data of it to a buffer and anther file.
#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!