CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2004
    Posts
    93

    Question 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.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured