1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Copy/Read/Write a binary file(.mp3)

Discussion in 'C' started by swapnaoe, Apr 21, 2008.

  1. swapnaoe

    swapnaoe New Member

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,
    The job is to read a .mp3 binary file and create(write) a new .mp3 file which is same as the original file. So, the basic thing we will be trying to do is to read/write of binary file(.doc, .pdf, .xls, .mp3). So the code should finally create a binary file(say a .mp3), which when opened with a player, should be recognized and played correctly.
    Has anyone worked on this?
    I mean, it will be appreciating if they can help me in getting started with this.

    Regards
    Swapna
     
  2. swapnaoe

    swapnaoe New Member

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    THought it would be useful for someone, so heres the code

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      filebuf *pbuf;
      ifstream sourcestr;
      ofstream desstr;
      long size;
      char * buffer;
    
      sourcestr.open("C:\\E_Drive\\Projects\\I-Radio\\Jashnebahaara.mp3",ios::in | ios::binary);
      desstr.open("C:\\E_Drive\\Projects\\I-Radio\\Jashnebahaara1.mp3", ios::out | ios::binary);
     
        // get pointer to associated buffer object
      pbuf=sourcestr.rdbuf();
    
      // get file size using buffer's members
      size=pbuf->pubseekoff (0,ios::end,ios::in);
      pbuf->pubseekpos (0,ios::in);
    
      // allocate memory to contain file data
      buffer=new char[size];
    
      // get file data  
      pbuf->sgetn (buffer,size);
    
      sourcestr.close();
    
      // write content to Jashnebahaara1.mp3
    
    desstr.write(buffer,size);
    desstr.close();
    
      return 0;
    }
     

Share This Page