Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Copy/Read/Write a binary file(.mp3) (http://www.go4expert.com/forums/copy-read-write-binary-file-mp3-t10158/)

swapnaoe 21Apr2008 11:58

Copy/Read/Write a binary file(.mp3)
 
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 22Apr2008 11:32

Re: Copy/Read/Write a binary file(.mp3)
 
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;
}



All times are GMT +5.5. The time now is 01:44.