How would this look if I used fstream?

This is a discussion on How would this look if I used fstream? within the C++ Programming forums, part of the General Programming Boards category; Hi ~ I found this code on Planet Source Code. It performs some file I/O to read ID3 tags in ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    How would this look if I used fstream?

    Hi ~

    I found this code on Planet Source Code. It performs some file I/O to read ID3 tags in MP3 files. Here it is:

    Code:
    class ID3struct
            {
            public:
                char TID[3];
                char title[30];
                char artist[30];
                char album[30];
                char year[4];
                char comment[30];
                BYTE genre;
    
            }
            ID3Data;
            {
                HANDLE hFile;
                DWORD read;
                char ID3[128];
                hFile = CreateFile("bond.mp3",  // file to read
                                   GENERIC_READ,   // open for reading
                                   0, // do not share
                                   NULL,// default security
                                   OPEN_EXISTING,  // open file if exists
                                   FILE_ATTRIBUTE_NORMAL, // normal file
                                   NULL);     // no attr. template
    
                SetFilePointer(hFile, -128, NULL, FILE_END);
                ReadFile(hFile, ID3, 128, &read, NULL);
                CloseHandle(hFile);
                memcpy(&ID3Data, ID3, 128);
    
            }
    I don't actually understand anything that is going on here... I wonder if anyone could herlp me put this into fstream? (i.e. using #include <fstream.h>)

    Thankyou so much, I hope that this isn't too much to ask...

    ~ Chris Howarth

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,814
    Substitute CreateFile() with your ifstream constructor, remembering to open the file in binary mode using std::ios::binary. Substitute SetFilePointer() with the seekg() member function, and ReadFile() with the read() member function.

    Oh, and it is <fstream>, not <fstream.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    Ah thankyou!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 11:21 AM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM

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