Problem with scanning file name with path for music player
Hi guys!
After I managed to make the MP3 player, not I am stuck on scanning a list of FilePath/FileName from a database file and using it to play the music.
Scanning the directory and recording all files in a file database works correctly. But I can not read from this file for some reason. I will nead this for my encryption software after this, which will also scan a directory and encrypt everything in it. I also may need to skip non media files somehow.
Code:
/************************************** Plays most music/vide files **********************************************/
int iPlaySound ()
{
// Local variables and initialization //
// User input and initial warnings //
//::MessageBox (hwnd, "The codecs must be installed.\n" , "Information:", MB_OK); /* MB_OK == Message box with button "OK" only. */
//iTemporaryVariable1 = GetWindowText (hHandle1, &caTemporaryVariable8 [0], 1024); // hHandle processes the input from the EDIT text box. Save from element 0 of the global variable "caTemporaryVariable10" to maximum 1024. // Add " gear" before updating th STATIC text box down with the value from the EDIT text box up.
//strcpy (caTemporaryVariable8, cpMediafileName);
//AddDashToPath (cpMediafileName, iInputFromUser); // Replace \ with \\ for the file path. Unnecessary, since its already done in
// the iScanDir () function.
char *cpErrorText = "DEBUG: Can not open the file in iPlaySound ().";
FILE *fpFilePointer;
char *cpFileOpenMode = "r";
char caFilePathName [4096] = "FilesList.cfc";
char cpMediafileName [4096];
fpFilePointer = fopen (caFilePathName, cpFileOpenMode);
if (fpFilePointer == NULL)
{
printf (cpErrorText);
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorText); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
}
while (fscanf(fpFilePointer, "%s", cpMediafileName) == 2) // Until we reach the end of the file, record every row in cpMediafileName.
{
printf ("DEBUG: Play sound.");
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Play"); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
sprintf (caTemporaryVariable9, "open \"%s\" type mpegvideo alias mp3", cpMediafileName);
//::MessageBox (hwnd, caTemporaryVariable9, "DEBUG:", MB_OK); /* MB_OK == Message box with button "OK" only. */
printf ("%s", caTemporaryVariable9);
// Processing //
mciSendString("close mp3", NULL, 0, NULL);// The close for the media file is at the beginning in order to close the old media file after a new has been loaded and allow the other toolbox functions.
mciSendString(caTemporaryVariable9, NULL, 0, NULL);
//mciSendString("play mp3 from 0 wait", NULL, 0, NULL);
//mciSendString("play mp3 from 0", NULL, 0, NULL);
mciSendString("play mp3", NULL, 0, NULL);
}
fclose (fpFilePointer); // All files must be closed.
return 0; // Status code for everything is OK.
}
Scan directory and record all file names with the path in FilesList.cfc.
Code:
/**************** Write to file *************************/
int iWriteToFile (char caText [4096], char caFileName [4096], char *cpFileOpenMode) // A file name and path with a maximum of 4096 characters.
{
// Local variables and initialization
FILE *fpFilePointer; // A pointer to data of type FILE in order to process the file.
char *cpErrorMessage = "Error:\n The file can not be opened!\nPossible reasons:\n Wrong permissions.\n The file path is not correct.\n The file name is not correct.\n Other\n.";
fpFilePointer = fopen(caFileName, cpFileOpenMode); // Open the file cpFileName in mode cpFileOpenMode.
// Generic error handling
if (fpFilePointer == NULL) // fopen will return NULL if it can not open the file.
{
printf (cpErrorMessage);
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorMessage); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
return -1; // Error code for can not open the file.
}
else
{
fputs (caText, fpFilePointer);
return 0;
}
// Finishing the function
fclose (fpFilePointer); // All files must be closed.
}
/**************** Scan for all the files in a directory *************************/
int iScanDir (char caDirectoryPath [4096], char *cpWriteMode)
{
// Local variables and initialization
char *cpErrorText = "ERROR:\n Could not open the current directory.\n";
char *cpFileNameInDirectory; // The file name scanned from the directory.
char cpFileNamePathInDirectory [4096]; // The file name scanned from the directory with the path added. Must be an
// or the program will close unexpectedly and the compiler will give an
// maybe uninitialized error.
char *cpFileNameToWriteTo = "FilesList.cfc"; // The file in which to write the list of file names from the directory.
struct dirent *de; // Pointer for directory entry.
AddDashToPath(caDirectoryPath, iInputFromUser);// The user input is in format C:\Path\File, for C it should be C:\\Path\\File
// or C:/Path/File.
DIR *dr = opendir(caDirectoryPath); // Example for opening program directory: DIR *dr = opendir(".");
if (dr == NULL) // opendir returns NULL if couldn't open directory. NULL is not null.
{
printf(cpErrorText); // C version.
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorText);// Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
return -2; // Error code for can not open the directory.
}
/************* !!!!!!!!! The file must be erased before the new directory is scanned. ************/
iWriteToFile("", cpFileNameToWriteTo, "w"); // Erases the file by opening it in write mode an writing nothing.
// The file is closed in the function iWriteToFile.
// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
while ((de = readdir(dr)) != NULL) // While the directory is readable.
{
cpFileNameInDirectory = (de->d_name); // Record the next scanned file name in cpFileNameInDirectory.
strcpy (cpFileNamePathInDirectory, caDirectoryPath);// Add the path: empty character pointer, path.
strcat (cpFileNamePathInDirectory, "/");// Add the directory path to the file name in the directory.
strcat (cpFileNamePathInDirectory, cpFileNameInDirectory);// Add the directory path to the file name in the directory.
// Directory path already in, file name.
strcat (cpFileNamePathInDirectory, "\n"); // Spearate all file names from the directory in rows.
//strcat (cpFileNamePathInDirectory, '\0'); // Only for debugging.
//printf("DEBUG %s\n", de->d_name); // Print the name of the next file in the directory.
iWriteToFile(cpFileNamePathInDirectory, cpFileNameToWriteTo, cpWriteMode);// Write the: file name scanned in the directory,
// into cpFileNameToWriteTo, mode for writing is append.
//SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)de->d_name);
//iPlaySound (de->d_name);
}
closedir(dr);
return 0;
}
/****************** Add \ to the file path, making it \\ or / *************************/
int AddDashToPath (char *cpInputfilename, int iFileOrInput)// Adds / to the file path, making it / and usable in programs, instead of the \.
{
unsigned int i = 0; // Counter.
char c; // Used for character reading.
FILE *ifp; // pointer to type file.
char *InMode = "r"; // Pointer to the mode for processing the file.
if (iFileOrInput == 99) // We will replace the dash in an input from the user, not from a file.
{
for (i = 0; cpInputfilename [i] != '\0'; i++) // Until the end of the user input in *cpInputfilename.
if (cpInputfilename [i] == '\\') // If we find a single dash, replace it with /.
cpInputfilename [i] = '/'; // Instead of / we can use \\.
}
else { // We will replace the dash on a file path in a file, not a user input.
ifp = fopen(cpInputfilename, InMode);
if (ifp == NULL)
{
return -1;
}
else
{
for (i = 0; (c = fgetc(ifp)) != EOF; i++)
if (c == '\\')
cpInputfilename [i] = '/';
}
}
return 0; // Code for all is ok.
}