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

This is a discussion on Copy/Read/Write a binary file(.mp3) within the C Programming forums, part of the General Programming Boards category; Hi all, The job is to read a .mp3 binary file and create(write) a new .mp3 file which is same ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    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

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Are you just trying to make an exact duplicate of a file?

    If so, there are many options in the methods of read/write. Why don't you give it a shot and if you have trouble along the way, someone here can take a look at your code.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Check out the functions fread() and fwrite()
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you only care about Windows, then CopyFile may be the right choice.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    There is a better way of doing this (read,write) but here is the code I used when making that program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    unsigned char byte;
    
    int main(void) {
        FILE *fin, *fout;
        int val;
        fin = fopen ("test.txt","rb");
        if (fin == NULL) {
            printf("Error opening input file\n");
            system("pause");
            exit(1);
        }
    
        fout = fopen ("newtext.txt","wb");
    
        if (fout == NULL) {
            printf("Error opening output file\n");
            system("pause");
            exit(1);
        }
    
        printf("Files opened, copying beginning.\n");
        while(1) { //infinant loop
            val = fgetc(fin);
            
            if (val == EOF)
                break;  
                
            byte = (unsigned char) val;
    
            
    
            fputc(byte,fout);
        }
    
    printf("Copy successful!\n");
    system("pause");
    
    }
    Last edited by epboyd; 04-21-2008 at 06:40 AM. Reason: typos

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Hardly very efficient tho' - if the file is large, you'll be making MANY calls to fgetc()/fputc(), where a fread/fwrite loop could easily reduce the amount of calling-overhead by a factor of a few thousand or more (you could read 1MB at a time, which would reduce the overhead by a factor of 1 million, roughly).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    That's what I meant by it could be better.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    THought it would be useful for someone, so heres the code for copying a binary file.

    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;
    }

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Shame that's not C...

    The downside to your method is you need at least size bytes free in memory to copy a file size bytes big. Waste of resources in my opinion. Do what matsp suggested.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Shame that's not C...

    The downside to your method is you need at least size bytes free in memory to copy a file size bytes big. Waste of resources in my opinion. Do what matsp suggested.
    Yes, and on a windows system, a file bigger than 2GB won't be possible to copy (unless you have large amounts of RAM _AND_ a 64-bit version of Windows, and compiled the code for 64-bit).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21