Hello I am pretty bad in C++ programming and i have this project to do. So program has to search through whole hard drive for MP3 files, and write to text file their name, path, and ID tags and made for Windows platform. Whole without using arrays. Can anyone help me a bit?
This is pretty straight forward, first you need to iterate over all the files/folders recursively, filter the ones with the extension you are interested in (in this case .mp3 files), and then read the meta data to get the ID tags.
This is how you can do it:
Iterating over the files and folders:
C++ doesn't provide a standard way of doing this, so you have to look into the different possible solutions available for you.
If you are working with Windows, you can use the Win32 API:
If you are working in Unix/Linux:
And if you want a cross platform solution you can look into boost filesystem module.
There are several implementations available over the internet using the former functions and methods.
Filtering the Files:
The general approach would be to split the string of the file path using the "." (dot) character as a separator and getting the last element (you will have to consider the case where the split is not successful due to the file not having dots).
Example (not tested):
bool fileHasExtension(const std::string& fileName, const std::string& extension)
{
if(fileName.find_last_of(".") != std::string::npos)
return extension.compare(fileName.substr(fileName.find_last_of(".") + 1)) == 0;
return false;
}
Getting the meta data:
You can read about it here http://id3.org/
An ID3 tag is a data container within an MP3 audio file stored in a prescribed format. This data commonly contains the Artist name, Song title, Year and Genre of the current audio file.
Example:
This is a working implementation of some of the concepts explained above, this program iterate recursively over all the files and folders and print on the screen the ones with the .mp3 extension (It does not get the meta data information on the ID3 tag).
#include <windows.h>
#include <string>
#include <iostream>
void GetFileListing(std::string directory, std::string fileFilter, bool recursively = true)
{
if (recursively)
GetFileListing(directory, fileFilter, false);
directory += "\\";
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
std::string filter = directory + (recursively ? "*" : fileFilter);
hFind = FindFirstFile(filter.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return;
}
else
{
if (!recursively)
{
std::cout << directory + std::string(FindFileData.cFileName) << std::endl;
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if (!recursively)
{
std::cout << directory + std::string(FindFileData.cFileName) << std::endl;
}
else
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
{
GetFileListing(directory + std::string(FindFileData.cFileName), fileFilter);
}
}
}
DWORD dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
std::cout << "FindNextFile error. Error is "<< dwError << std::endl;
}
}
}
int main(int argc, char* argv[])
{
GetFileListing("C:\\", "*.mp3");
}
It uses Windows API, so if you are working with Linux or just want a cross platform solution, you will have to change the code.