1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ assignment with .mp3 files

  1. Feb 25, 2005 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    OK, so I've got this wacky homework assignment that involves reading stuff out of a .mp3 file. Just to get started, I am attempting to read the first four "bytes" of the file and put them into an (unsigned char) array. I am not really sure if I am reading from the file or not - I can't tell from these characters that I print out. Does this look right to you?

    Code (Text):

    # include <iostream>
    # include <fstream>

    using namespace std;
    typedef unsigned char byte;

    int main()
    {
        cout<< "enter the mp3 filename (including the .mp3): "<< endl;
        char buff[100];
        cin.getline(buff,100);
        byte array[4];
        ifstream in(buff, ios::in|ios::binary);

        if(!in)
        {
            cout <<"no file found" << endl;
            exit(1);
        }

        for (int i = 0; i<4;++i)
        {
            char ch;
            in.get(ch);
            array[i] = ch;
        }

        in.close();

        for (int j = 0; j<4;++j)
            cout << array[j];
        cout <<endl;

        return 0;
    }
     
    Here's the assignment I am working on by the way.
    http://www.math.ucla.edu/~rclark/10a.1.05w/hw5/hw5.html
    The teacher has written most of this for us and there's really just a few functions I have to write but I have been a nervous wreck trying to figure this out. I thought I better go ahead and post since I know I'll be working on it all weekend. :frown:
    More questions coming soon.
     
  2. jcsd
  3. Feb 25, 2005 #2
    Ok the code looks fine to me.
    You should be able to check to see if you are reading the right stuff by opening up the mp3 file with notepad, vi, wordpad, and checking to see if the first three chars are the same. This has worked for me in the past.

    I would also use in.is_open() rather then !in. In your library are these the say thing?
     
    Last edited: Feb 25, 2005
  4. Feb 25, 2005 #3

    saltydog

    User Avatar
    Science Advisor
    Homework Helper

    Also:

    Save a file that you know what's in it. You know, enter some text in a word processor, save as a .txt file, then read it in with your program and verify it's getting the data.

    And, well, nice if you can run it in a debugger like Microsoft Visual C++ environment.
     
  5. Feb 25, 2005 #4

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks for the hints, y'all. I am so lost on this assignment for some reason!
     
  6. Feb 26, 2005 #5

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    MIH,

    Do you have to use the silly C++ stream library functions to do this assignment? They are reeeeeally not designed nor appropriate for reading binary files.

    You'd be much better off just using the fopen() and fread() functions to directly read four bytes into an array. In fact, your code will fail if one of the first four characters is a newline character. Your call to getline() will then return fewer than four bytes, and your code with throw an exception before you read all four.

    http://www.opengroup.org/onlinepubs/007908799/xsh/fopen.html
    http://www.opengroup.org/onlinepubs/007908799/xsh/fread.html

    - Warren
     
  7. Feb 26, 2005 #6

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Hi Warren,

    yeah, I believe I am pretty much stuck with this. actually the teacher has already written most of the program and he uses <fstream> - our assignment is just to write some of the functions in the program. I believe the main point of this assignment was to introduce us to to using ifstream and ofstream.
    Thanks for telling me about those functions though. I will probably need them in the future. It does sound like that would be a better way to go for working with binary files.
     
  8. Feb 26, 2005 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Don't forget about the istream.read method, which is directly analogous to fread.
     
  9. Feb 26, 2005 #8

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    ok, here come some questions. The first one is about writing these "bytes" in my array out to a binary file.
    If I make an ofstream variable called "out" can I just do this?

    out.put(array[someIndexNumber]);

    when I take the stream in, I capture each "byte" as a character and put it into the unsigned char array. I am not sure what I need to do before sending it back out to a different binary file.
    Thanks for your advice!
     
  10. Feb 26, 2005 #9

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I also asked plover this question earlier in a PM. I'll post it here in case he wants to reply in this post:
    *************************************************
    I am confused about this "string" data type in the return type of the function below which I am supposed to complete:

    /* Instructions: if i<0, returns the string "0". for all others, it returns the string version of i. */

    string int2string(int i)
    {
    // write this stuff
    }

    OK, I am going to guess and say we're able to use this string datatype because we're using the string class - we didn't cover very much about this yet. But I don't know how to use it. Is it like a char array?

    If it's like an array, then I am confused about using it for a return type. :confused:
     
  11. Feb 26, 2005 #10
    Do you mean the standard std::string class? man std::string may help you.
     
  12. Feb 26, 2005 #11

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The string datatype is part of the Standard Template Library. You can wrap a char* into a std::string with one of the std::string constructors like this:

    char buf[100];
    sprintf(buf, "%d", i);
    return string str(buf);

    - Warren
     
  13. Feb 26, 2005 #12

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks, Warren. I'll play around with that. So I guess regular old arrays can't be return types but strings can?
     
  14. Feb 26, 2005 #13

    plover

    User Avatar
    Homework Helper

    You can return an array that is created in a function, but it must have been created on the heap with malloc() and you have to remember to free this memory when the array is no longer useful.

    Using an object just makes this whole process conceptually simpler. (Though if done without a little care, it can also make the process a lot less efficient.) There ought to be a reference section on the string class in your book.

    danne89 is referring to the unix documentation system (known as 'man pages' as the docs are accessed through command called 'man' (for manual)).

    The string class while part of the C++ Standard Library, is not technically part of the C++ Standard Template Library (though it is closely related).

    While it is certainly possible to initialize a string with a char array, this does not simply create an object wrapper around the array—the string object will create it's own storage space and copy the array. One useful way to create a string from a character is to use the constuctor that's intended to create a string consisting of n copies of a given character:
    Code (Text):
    char c = 'x';
    string s(1, c);
     
    Also the 'return' line in chroot's example should be:
    Code (Text):
    return string(buf);
     
    When returning an object, applying 'return' to the result of a constructor call like this is often the way to go.

    As to the earlier question, if you are writing to your output file one byte at a time, using the 'put' member function is just fine.
     
    Last edited: Feb 26, 2005
  15. Feb 27, 2005 #14

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks so much, plover. I am having pretty good luck with the string constructor. Almost done! Then the debugging begins - pleh!! :yuck:
    I have to whip up a little something to take an integer and turn it into an array of characters, (will convert it to a string afterwards) but I did something similar in another assignment so hopefully I can figure that out. I don't suppose there's any slick little function that would do that for me? nah, probably not. I couldn't find one anyway.
    thanks again. I'll let you know how I make out. :biggrin:
     
  16. Feb 27, 2005 #15

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

  17. Feb 27, 2005 #16

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks for that link, Warren. That was really extensive! Much more than what is in my book.

    I am still stuck on a little problem. I can take and integer like 155 and convert it to an int array of single digit integers 1,5,5, but I am not sure how to go from there to convert it into an array of characters '1','5', and '5'. This is what I want to do before I construct the string.

    I found some suggestions on the internet for taking an integer and converting it directly to a character array using itoa(), but my version of C++ didn't like that. :frown:

    If I could just figure out how to take a single digit integer and convert it to a single character, that would be helpful, but I am unable to figure that out from my book, or from the the docs I have found on the internet.
    Any advice? Thanks.
     
  18. Feb 27, 2005 #17

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    If you can't make itoa work, then just use sprintf. Do not attempt to do the conversion to characters yourself, it's a total waste of time.

    By the way, the ASCII codes for the numerals 0-9 are 0x30 through 0x39. If you want to convert a single-digit number to its ASCII equivalent, just add 0x30 to it and cast it to char.

    - Warren
     
  19. Feb 27, 2005 #18

    plover

    User Avatar
    Homework Helper

    To convert a single digit, you can always do this:
    Code (Text):
    int d = getDigit();
    char c = '0' + d;
     
    But the entire number can be converted to a char array at once using the C 'sprintf' function from <cstdio> (as in chroot's example above), or directly to a string using C++ <sstream> library.

    edit: oops, chroot got here first...
     
    Last edited: Feb 27, 2005
  20. Feb 27, 2005 #19

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks for the help, chroot and plover! :smile:
     
  21. Feb 27, 2005 #20

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    # include <bighugs>
    HOORAYYYY!! It works!!!!
    *jumps for joy*
    I have the whole program working now. (well, I haven't compiled it in .NET version of MS VC++ but all is A-OK here in version 6, and I worked really hard to avoid any incompatibility issues that have given me headaches before.)
    Couldn't have done it without y'all. Thanks again!!!!!! :smile:
    <hug><hug><hug>
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ assignment with .mp3 files
  1. Interesting assignment (Replies: 4)

  2. Kinematics Assignment (Replies: 2)

  3. Assignment on friction (Replies: 1)

  4. Springs Assignment (Replies: 11)

Loading...