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

swapnaoe's Avatar
Go4Expert Member
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
swapnaoe's Avatar
Go4Expert Member
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;
}