Thread: copying a mp3 file

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MWAAAHAAA View Post
    Nah, I think you couldn't find the delete button, so you're trying to save face by putting in an edit.
    And people wonder why I have him on my ignore list...

  2. #17
    Registered User shubham's Avatar
    Join Date
    May 2011
    Posts
    14
    hey guys.....i want some more help from you....i justwant to calculate the size of 17.mp3 in mb or kb......so what will i do ...count every int then calculate or there is an another way out.......

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by CommonTater View Post
    And people wonder why I have him on my ignore list...
    And I'm such a likable fellow...
    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

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by shubham View Post
    hey guys.....i want some more help from you....i justwant to calculate the size of 17.mp3 in mb or kb......so what will i do ...count every int then calculate or there is an another way out.......
    Have a look at CommonTater's first post for a possible approach.
    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

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shubham View Post
    hey guys.....i want some more help from you....i justwant to calculate the size of 17.mp3 in mb or kb......so what will i do ...count every int then calculate or there is an another way out.......
    Take a look at the code sample I posted... there's a very easy way to do it, in there.

    Once you have the file size divide by 1024 for Kilobytes or 1048576 for Megs.
    Last edited by CommonTater; 05-29-2011 at 05:41 PM.

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I've malloced gigabyte size buffers many times with no problems
    If you wanted this data in memory for a long time, then that would be fine.

    But copying a 1GB file by allocating one large block of memory is basically a big waste of resources (and possibly even slower than you imagine).

    For one thing, few operating systems will commit 1GB of REAL memory to a process which has only written to each page only once. Which means a large chunk of that memory you just loaded with file data is going straight back out to the swap file.

    Then it comes back in from the swap file and out to the place where you wanted to save it.

    It's also prone to random failure. One day you might be able to allocate enough space for a particular file. But maybe tomorrow, there is an extra process on the OS, or the file is just a little bit larger, and your attempt to allocate one stupidly large buffer returns NULL instead.
    What's your error message to the user at this point?
    "Sorry, unable to copy file due to ..."

    Try this
    Code:
    unsigned char buf[BUFSIZ];
    size_t n;
    while ( (n=fread(buf, 1, sizeof(buf), in ) > 0 ) {
      size_t r = fwrite( buf, 1, n, out );
      if ( r != n ) {
        // some error message, probably no space left
        // use ferror
        break;
      }
    }
    No malloc/free issues, no swap file thrashing, works for files of any size.

    And don't play that "It's only a small MP3 file" crap, because you know some doofus will just look at that code and think "Hey, it worked for MP3, what about AVI files?".
    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.

  7. #22
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I agree with salem, reading the whole file does not always make faster.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > I've malloced gigabyte size buffers many times with no problems
    If you wanted this data in memory for a long time, then that would be fine.

    But copying a 1GB file by allocating one large block of memory is basically a big waste of resources (and possibly even slower than you imagine).

    For one thing, few operating systems will commit 1GB of REAL memory to a process which has only written to each page only once. Which means a large chunk of that memory you just loaded with file data is going straight back out to the swap file.

    Then it comes back in from the swap file and out to the place where you wanted to save it.

    It's also prone to random failure. One day you might be able to allocate enough space for a particular file. But maybe tomorrow, there is an extra process on the OS, or the file is just a little bit larger, and your attempt to allocate one stupidly large buffer returns NULL instead.
    What's your error message to the user at this point?
    "Sorry, unable to copy file due to ..."

    Try this
    Code:
    unsigned char buf[BUFSIZ];
    size_t n;
    while ( (n=fread(buf, 1, sizeof(buf), in ) > 0 ) {
      size_t r = fwrite( buf, 1, n, out );
      if ( r != n ) {
        // some error message, probably no space left
        // use ferror
        break;
      }
    }
    No malloc/free issues, no swap file thrashing, works for files of any size.

    And don't play that "It's only a small MP3 file" crap, because you know some doofus will just look at that code and think "Hey, it worked for MP3, what about AVI files?".
    Ok... I see your point and I've done things that way too... but with decent sized buffers ... 256k or such... not character by character...

  9. #24
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by CommonTater View Post
    Ok... I see your point and I've done things that way too... but with decent sized buffers ... 256k or such... not character by character...
    The problem is that you gave him an example which may result in bad habits and severely affect his coding style. Think what can happen in the future if he works as a programmer in a big company. He may be involved in a large desktop application sold to thousands of customers worldwide.

    Suddenly it will crash.

    Do you want to be responsible for this?

    Shame on you.

    :o

  10. #25
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Copying by reading byte by byte and reading all are two extremes that you should avoid. LOL

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmdv View Post
    The problem is that you gave him an example which may result in bad habits and severely affect his coding style. Think what can happen in the future if he works as a programmer in a big company. He may be involved in a large desktop application sold to thousands of customers worldwide.

    Suddenly it will crash.

    Do you want to be responsible for this?

    Shame on you.

    (SIGH!) Salem at least had the decency to explain ....

    I guess I shouldn't mention that I have programs out there that open 800meg to 1gb files, work on them and then write them back out, all in one big blob... And have been doing that for YEARS without problems...

  12. #27
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Doing something wrong years does not make it right.
    May be your program always run fine on same machine, same platform, etc... you need to consider... these all.(environment that your code will run).
    My laptop has only 1GB of memory. Do you know how much my system hangs when I allocate just 500MB with malloc()?

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    Doing something wrong years does not make it right.
    May be your program always run fine on same machine, same platform, etc... you need to consider... these all.(environment that your code will run).
    My laptop has only 1GB of memory. Do you know how much my system hangs when I allocate just 500MB with malloc()?
    Tell ya what... in anything that got beyond my own hard disk, I would have --and always do-- include a lot more error checking. I was trying to give the OP a concept --an idea-- not redistributable code...

    In any case, the point is made, and I agree a smaller buffer and a loop would have been better...

    In future I will mark these things with "Provided to show concept only"

  14. #29
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes I know the code is not important. but the concept in the code is not really a good one.
    Edit: I think there's no point continuing discussion either.
    Last edited by Bayint Naung; 05-30-2011 at 09:34 AM.

  15. #30
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    Yes I know the code is not important. but the concept in the code is not really a good one.
    Edit: I think there's no point continuing discussion either.
    It's fine to continue far as I'm concerned... but maybe we should get back to the OP and his problem...

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, 02:15 PM
  3. Copying a File
    By thatdude in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2003, 07:07 AM
  4. copying a file
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-10-2002, 09:43 AM
  5. Copying a file
    By rkjd2 in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2001, 10:24 AM