This is my hole code:
Code:
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
using std::ios;
using std::filebuf;
class CID3 {
public:
CID3() : _strFile("") {};
CID3(const string& strMP3File) : _strFile(strMP3File) {}
~CID3() {
_iFile.close();
}
bool bOK() {
_iFile.open(_strFile.c_str(), ios::binary);
_bOk = _iFile.good();
return _bOk;
}
int GetBytes() {
pbuf = _iFile.rdbuf();
_bytes = pbuf->pubseekoff(0, ios::end); // FileSize
return _bytes;
}
int Parse() {
pbuf->pubseekpos(_bytes-128);
string strTag;
char szTag[3], szTrackName[30], szTrackArtist[30], szTrackAlbum[30], szTrackYear[4], szTrackComment[30], szTrackGenre[1];
pbuf->sgetn(szTag, 3);
strTag = szTag;
if (strTag == "TAG") { // ID3v1.0
// Title
pbuf->sgetn(szTrackName, 30);
_strTitle = szTrackName;
// Artist
pbuf->sgetn(szTrackArtist, 30);
_strArtist = szTrackArtist;
// Album
pbuf->sgetn(szTrackAlbum, 30);
_strAlbum = szTrackAlbum;
// Year
pbuf->sgetn(szTrackYear, 4);
_strYear = szTrackYear;
// Comment
pbuf->sgetn(szTrackComment, 30);
_strComment = szTrackComment;
// Genere
pbuf->sgetn(szTrackGenre, 1);
_strGenre = szTrackGenre;
return 1;
}
return 0;
}
string GetTitle() const {
return _strTitle;
}
string GetArtist() const {
return _strArtist;
}
string GetAlbum() const {
return _strAlbum;
}
string GetYear() const {
return _strYear.substr(0, 4);
}
string GetComment() const {
return _strComment;
}
string GetGenre() const {
return _strGenre;
}
private:
string _strGenre; // Genre
int _Genre; // # Genre
string _strComment; // Comment
string _strYear; // Year
string _strAlbum; // Album
string _strArtist; // Artist
string _strTitle; // Title
string _strFile;
ifstream _iFile;
filebuf *pbuf;
int _bytes;
bool _bOk;
};
int main() {
CID3 pMP3("X:\\mp3s\\chris isaak - chris issac - wicked game.mp3");
if (pMP3.bOK()) {
cout << "OK" << endl;
cout << pMP3.GetBytes() << endl;
cout << pMP3.Parse() << endl;
cout << "[" << pMP3.GetTitle() << "]" << endl; // Works!
cout << "[" << pMP3.GetArtist() << "]" << endl; // Works!
cout << "[" << pMP3.GetAlbum() << "]" << endl; // Works!
cout << "[" << pMP3.GetYear() << "]" << endl; // Works, but with a extra "+" sign
cout << "[" << pMP3.GetComment() << "]" << endl; // "Works"
cout << "[" << pMP3.GetGenre() << "]" << endl; // No integer value returns
}
else {
cout << "Error!" << endl;
}
return 0;
}