Like Tree5Likes

copying a mp3 file

This is a discussion on copying a mp3 file within the C Programming forums, part of the General Programming Boards category; help me...i am copying a mp3 file....but when i run the code the copied file is having larger size than ...

  1. #1
    Registered User shubham's Avatar
    Join Date
    May 2011
    Posts
    14

    copying a mp3 file

    help me...i am copying a mp3 file....but when i run the code the copied file is having larger size than the original mp3 file.....here is the code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        
        FILE *fs,*ft;
        
        unsigned char ch;
        
        fs=fopen("17.mp3","rb");
        if(fs==NULL)
        {
                    puts("cannot open source file");
                    exit(1);
        }
        ft=fopen("16.mp3","wb");
        if(ft==NULL)
        {
                    puts("cannot open target file");
                    fclose(fs);
                    exit(2);
        }
        while(1)
        {
                ch=fgetc(fs);
                if(ch==EOF)
                break;
                else
                fputc(ch,ft);
        }
        fclose(fs);
        fclose(ft);
        return(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    ch should be an int, if you want to compare it with EOF successfully.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User shubham's Avatar
    Join Date
    May 2011
    Posts
    14
    thanks salem.....it is working by taking ch as int
    thanks again

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        FILE *fs,*ft;
        unsigned int size;
        unsigned char* buffer;
            
        fs=fopen("17.mp3","rb");
        if(fs==NULL)
        {
                    puts("cannot open source file");
                    exit(1);
        }
        ft=fopen("16.mp3","wb");
        if(ft==NULL)
        {
                    puts("cannot open target file");
                    fclose(fs);
                    exit(2);
        }
         
        fseek(fs,0,SEEK_END);  // get file size
        size = ftell(fs);
        fseek(fs,0,SEEK_SET);
    
        buffer = malloc(size);  // allocate buffer space
      
        fread(buffer,size,1,fs);  // copy the file
        fwrite(buffer,size,1,ft);
    
        free(buffer);
        fclose(fs);
        fclose(ft);
        return(0);
    }
    Last edited by CommonTater; 05-29-2011 at 02:49 PM. Reason: added free....

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Is it worth pointing out the flaws in CommonTater's approach?

    Maybe tomorrow, lets see if CommonTater can make a better job.
    Or perhaps even spot why the concept is flawed to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Is it worth pointing out the flaws in CommonTater's approach?

    Maybe tomorrow, lets see if CommonTater can make a better job.
    Or perhaps even spot why the concept is flawed to begin with.
    Oh go ahead... you know you want to... Yes I forgot to free the buffer.... what else?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by CommonTater View Post
    Oh go ahead... you know you want to... Yes I forgot to free the buffer.... what else?
    Think BIG.
    Salem likes this.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MWAAAHAAA View Post
    Think BIG.
    What? MP3 files are virtually always under 100megs, most are under 10 ... that should be no problem unless you're on a DOS box...

    I've malloced gigabyte size buffers many times with no problems...

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    Well, as long as we are posting alternative source...

    Soma

    Code:
    #include <fstream>
    
    int main()
    {
        using namespace std;
        ifstream in("17.mp3", ios::in | ios::binary);
        ofstream out("16.mp3", ios::out | ios::binary);
        out << in.rdbuf();
        return(0);
    }
    Last edited by phantomotap; 05-29-2011 at 03:00 PM. Reason: ^_^;

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by phantomotap View Post
    O_o

    Well, as long as we are posting alternative source...

    Soma

    Code:
    #include <fstream>
    
    int main()
    {
        using namespace std;
        ifstream in("17.mp3", ios::in | ios::binary);
        ofstream out("16.mp3", ios::out | ios::binary);
        out << in.rdbuf();
        return(0);
    }
    Except that this is the C programming forum
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    That being the reason that is was a joke... see the smiley?

    Soma

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by phantomotap View Post
    O_o

    Well, as long as we are posting alternative source...

    Soma

    Code:
    #include <fstream>
    
    int main()
    {
        using namespace std;
        ifstream in("17.mp3", ios::in | ios::binary);
        ofstream out("16.mp3", ios::out | ios::binary);
        out << in.rdbuf();
        return(0);
    }
    C forum says hello!
    I never put signature, but I decided to make an exception.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    I was late...

    Quote Originally Posted by phantomotap View Post
    That being the reason that is was a joke... see the smiley?

    Soma
    I can't, where is this???
    Last edited by kmdv; 05-29-2011 at 03:20 PM. Reason: Ok I can see...
    I never put signature, but I decided to make an exception.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by phantomotap View Post
    That being the reason that is was a joke... see the smiley?

    Soma
    Nah, I think you couldn't find the delete button, so you're trying to save face by putting in an edit.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    Nah, I think you couldn't find the delete button, so you're trying to save face by putting in an edit.
    Along that path lies boredom.

    Soma

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Copying
    By jerez_z in forum C Programming
    Replies: 0
    Last Post: 06-02-2004, 05:44 PM
  2. Copying a file
    By shivam in forum Windows Programming
    Replies: 2
    Last Post: 12-22-2003, 01:15 PM
  3. Copying a File
    By thatdude in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2003, 06:07 AM
  4. copying a file
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-10-2002, 08:43 AM
  5. Copying a file
    By rkjd2 in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2001, 10:24 AM

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