Thread: Problem with scanning file name with path for music player

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    96

    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.
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (fscanf(fpFilePointer, "%s", cpMediafileName) == 2)
    A single conversion isn't ever going to return 2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    When I use EOF, I receive ISO++ does not allow conversion between pointer and integer. How do you suggest I make it?

    EDIT:
    For some reason the EOF works now, but still the software does not enter the while loop.
    Last edited by ArakelTheDragon; 04-19-2020 at 05:39 AM. Reason: Add information

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That error message does not make sense since fscanf returns an integer, and EOF is also an integer, so you would need to post the relevant updated code to be sure. Having said that, the right thing to do was to compare with 1 instead of 2.
    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

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    With 1, 2 and EOF the software does not enter the loop cycle.

    Full code:
    Code:
    #if defined(UNICODE) && !defined(_UNICODE)
        #define _UNICODE
    #elif defined(_UNICODE) && !defined(UNICODE)
        #define UNICODE
    #endif
                                                        // Pragmas //
    #pragma GCC diagnostic ignored "-Wwrite-strings"    // Suppress  the `deprecated conversion from string constant to ‘char*’` warning.
    
                                                        // Built-in library includes //
    #include <windows.h>                                // Must always be included first.
    //#include <mmsystem.h>                             // Must always be included second. Necessary for PlaySound (). Its included in "Windows.h".
    #include <iostream>
    #include <dirent.h>
    #include <inttypes.h>                               // Necessary for format identifiers like %llu, %lld and etc. %llu can be replaced with %I64u.
    #include <tchar.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #include <math.h>                                   // For iQuadraticEquation (void).
    
    #define iInputFromUser 99
    
    using namespace std;
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
    
                                                    // Function declarations //
    int iWriteToFile (char caText [4096], char caFileName [4096], char *cpFileOpenMode); // Writes to a file a text with maximum
                                                                                         // length of 4096 characters, a file path
                                                                                         // name with a maximum length of 4096
                                                                                         // characters and mode for opening the file.
    int iScanDir (char caDirectoryPath [4096], char *cpWriteMode);// Scan a directory. and write all file names from it to a file in mode.
    int AddDashToPath (char *cpInputfilename, int iFileOrInput);
    int iPlaySound ();                              // Plays a sound.
    int iFindFactorial(void);                       // Find the factorial.
    int iQuadraticEquation (void);                  // Calculates the roots of a quadratic equation.
    int iEvaluateCharacter();                       // Evaluates if a character is a vowel or a constant.
    int AddRequestToDatabase (char *cpStringInput);
    int ReadRequestFromDatabase (char *cpStringInput, char *cpReturnedString);
    
                                                    // Global variables
    short shortTemporaryVariable1 = 0;
    int iTemporaryVariable4 = 0;
    int iTemporaryVariable3 = 0;
    int iTemporaryVariable2 = 0;
    int iTemporaryVariable1 = 0;
    int ipTemporaryVariable2 = 0;
    int ipTemporaryVariable1 = 0;
    long liTemporaryVariable1 = 0;                  // equivalent to long int;
    long long llTemporaryVariable1;                 // equivalent to long long int c;
    double dTemporaryVariable1;
    long double ldTemporaryVariable1;
    float fTemporaryVariable1 = 0.0;
    char cTemporaryVariable1;
    char caTemporaryVariable11 [1024];
    char caTemporaryVariable10 [1024];
    char caTemporaryVariable9 [1024];
    char caTemporaryVariable8 [1024];
    char *cpTemporaryVariable2;
    char *cpTemporaryVariable1;
    char *cpInformation3 = "The INCA variables always correspond to the actual parameter value, they can not be changed except by calibrations, which is forbidden without an approval from your team leader. A change by calibrations is rarely needed.\0";
    char *cpInformation2 = "The boxes on the left are the ControlDesk controls. They are updated by pressing update on the right.\0";
    char *cpInformation1 = "The INCA variables can be set by either the ControlDesk controls or the ControlDesk BUS signals. As long as the BUS is active, the BUS signals are with priority and all values must be the same.\0";
    char *cpInformationMessage1 = "The codecs must be installed.\n";
    
    HWND hwnd;                                        // Generic handle for our window.
    
                                                      // Handle for STATIC windows
    HWND hHandle210;                                  // Generic handle for all functions.
    HWND hHandle209;                                  // Generic handle for all functions.
    HWND hHandle208;                                  // Generic handle for all functions.
    HWND hHandle207;                                  // Generic handle for all functions.
    HWND hHandle206;                                  // Generic handle for all functions.
    HWND hHandle205;                                  // Generic handle for all functions.
    HWND hHandle204;                                  // Generic handle for all functions.
    HWND hHandle203;                                  // Generic handle for all functions.
    HWND hHandle202;                                  // Generic handle for all functions.
    HWND hHandle201;                                  // Generic handle for all functions.
    HWND hHandle200;                                  // Generic handle for all functions.
    
                                                      // Handle for BUTTON windows
    HWND hHandle107;                                  // Generic handle for all functions.
    HWND hHandle106;                                  // Generic handle for all functions.
    HWND hHandle105;                                  // Generic handle for all functions.
    HWND hHandle104;                                  // Generic handle for all functions.
    HWND hHandle103;                                  // Generic handle for all functions.
    HWND hHandle102;                                  // Generic handle for all functions.
    HWND hHandle101;                                  // Generic handle for all functions.
    HWND hHandle100;                                  // Generic handle for all functions.
    
                                                      // Handles for EDIT windows
    HWND hHandle9;                                    // Generic handle for all functions.
    HWND hHandle8;                                    // Generic handle for all functions.
    HWND hHandle7;                                    // Generic handle for all functions.
    HWND hHandle6;                                    // Generic handle for all functions.
    HWND hHandle5;                                    // Generic handle for all functions.
    HWND hHandle4;                                    // Generic handle for all functions.
    HWND hHandle3;                                    // Generic handle for all functions.
    HWND hHandle2;                                    // Generic handle for all functions.
    HWND hHandle1;                                    // Generic handle for all functions.
    
    int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) // HINSTANCE is a handle to an instance, its a typedefstruct, has the same role as an ID.
    {                                                                                                       // hPrevInstance is an ID for bacwards compatibility only its not relevant for today's computers.
                                                                                                            // LPSTR lpszArgument is a pointer to a char
                                                                                                            // int nCmdShow = how your program is displayed.
        //HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default colour as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               _T("Simple media player"),       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               650,                 /* The programs width */
               130,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nCmdShow);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
                                                                /**** Local variables *****/
    
        switch (message)                  /* handle the messages */
        {
    /////////////////////////////////////////////////////////////////////////////////////////////////
                                     // This "case" creates the graphical interface.
            case WM_CREATE:
    
            hHandle200 = CreateWindow("Static",  "The codecs must be installed!\n",
                                    WS_CHILD | WS_VISIBLE | SS_LEFT,
                                    10, 60, 415, 20,
                                    hwnd, (HMENU) 200, NULL, NULL);
            hHandle104 = CreateWindow ("BUTTON",                       // There is no need for a handle for a simple button. Element type "BUTTON".
                          "Skip",                                      // Element displayed text "SEND".
                          WS_VISIBLE | WS_CHILD | WS_BORDER,           // Has border, is child window, is visible.
                          430, 35, 100, 20,                            // Left from main window end, Down main from window top, Width, Hight.
                          hwnd,(HMENU) 104, NULL, NULL);
            hHandle103 = CreateWindow ("BUTTON",                       // There is no need for a handle for a simple button. Element type "BUTTON".
                          "Stop",                                      // Element displayed text "SEND".
                          WS_VISIBLE | WS_CHILD | WS_BORDER,           // Has border, is child window, is visible.
                          325, 35, 100, 20,                           // Left from main window end, Down main from window top, Width, Hight.
                          hwnd,(HMENU) 103, NULL, NULL);
            hHandle102 = CreateWindow ("BUTTON",                       // There is no need for a handle for a simple button. Element type "BUTTON".
                          "Resume",                                 // Element displayed text "SEND".
                          WS_VISIBLE | WS_CHILD | WS_BORDER,           // Has border, is child window, is visible.
                          220, 35, 100, 20,                           // Left from main window end, Down main from window top, Width, Hight.
                          hwnd,(HMENU) 102, NULL, NULL);
            hHandle101 = CreateWindow ("BUTTON",                       // There is no need for a handle for a simple button. Element type "BUTTON".
                          "Pause",                                // Element displayed text "SEND".
                          WS_VISIBLE | WS_CHILD | WS_BORDER,           // Has border, is child window, is visible.
                          115, 35, 100, 20,                           // Left from main window end, Down main from window top, Width, Hight.
                          hwnd,(HMENU) 101, NULL, NULL);
            hHandle100 = CreateWindow ("BUTTON",                       // There is no need for a handle for a simple button. Element type "BUTTON".
                          "Play",                              // Element displayed text "SEND".
                          WS_VISIBLE | WS_CHILD | WS_BORDER,           // Has border, is child window, is visible.
                          10, 35, 100, 20,                            // Left from main window end, Down main from window top, Width, Hight.
                          hwnd,(HMENU) 100, NULL, NULL);
            hHandle1 = CreateWindow ("EDIT",                            // User unput text box type child windows. "hHandle" is the handle name.
                                    "The codecs must be installed! Enter song path here.",
                                    WS_BORDER | WS_CHILD | WS_VISIBLE,  // Has border, is child window, is visible.
                                    10, 10, 415, 20,                    // Left from main window end, Down main from window top, Width, Hight.
                                    hwnd,                               // Handle name to handle for our main window.
                                    (HMENU) 1, NULL, NULL);
            break;                                                      // The break ends the message after the functionality we added is executed.
    
    /////////////////////////////////////////////////////////////////////////////////////////////////
                                            // When the media playing finishes, this case will be executed
           case MM_MCINOTIFY:
    
               break;
                                            // This "case" is the actual action performed by our graphical elements.
           case WM_COMMAND:
            switch (LOWORD(wParam))         // Local variables can not be added in the cases themselves.
            {
                case 104:
                                            // User input
                    mciSendString("skip mp3", NULL, 0, NULL);             // Pause the currently playing file.
                    SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Skip");               // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
    
                break;
                                         // Stops the playing of the media file, after which it has to start from the beginning
                case 103:
                    mciSendString("stop mp3", NULL, 0, NULL);// Stop the currently playing file.
                    SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Stop");               // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
                break;
    //////////////////////////////////////////////////////////////////////////////////////////////////////
                                                            // Resume the media file
                case 102:
                    mciSendString("resume mp3", NULL, 0, NULL);             // Pause the currently playing file.
                    SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Resume");               // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
                break;
    //////////////////////////////////////////////////////////////////////////////////////////////////////
                                                            // Pause the media file
                case 101:
                    mciSendString("pause mp3", NULL, 0, NULL);             // Pause the currently playing file.
                    SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Pause");               // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
                break;
    //////////////////////////////////////////////////////////////////////////////////////////////////////
                                                            // Prints Hello world! in the static box
                case 100:
                    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.
                    iScanDir (caTemporaryVariable8, "a");
                    iPlaySound ();
    
                break;
    //////////////////////////////////////////////////////////////////////////////////////////////////////
            }
            break;
    
    /////////////////////////////////////////////////////////////////////////////////////////////////
                                           // Last case for exiting the program
            case WM_DESTROY:
                PostQuitMessage (0);       // send a WM_QUIT to the message queue
                break;
            default:                      // for messages that we don't deal with
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    
    /**************** 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.
    }
    
    /************************************** Plays most music/ video files **********************************************/
    // In order for the function to work, you must download and add Winmm.dll to Build options -> linker settings.
    // You must also add -lwinmm to Settings -> Compiler -> Linker settings -> Other linker options.
    
    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.
    }
    
    SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"DEBUG: Before while cycle.");               // 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) == 1) // 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.
    }
    
    /********************************** Processing the request *********************************************/
    int ReadRequestFromDatabase (char *cpStringInput, char *cpReturnedString)
    {
                                                        // Local variables //
        //char caStringInput [1024];                      // The request to be searched for will be stored in this character array.
        char caLineStorage[1024];                       // Declare storage for 1 line  of type char.
        FILE *fpFilePointer;                            // Declare a pointer to a type file.
    
                                                        // User input //
        //puts ("This program simulates an ECU diagnostic.\n");
        //puts ("1. Please enter the request to search for with no spaces!\n");
        //gets (caStringInput);
                                                        // File opening //
        fpFilePointer=fopen("database.cfc","r");        // Open the file for reading.
    
                                                        // Error handling for file opening.
        if(fpFilePointer == NULL) {                     // If the file pointer is NULL.
            perror("Unable to open file!\n");           // Print error text.
            return 1;                                   // Exit with error code 1.
        }
                                                        // Function calls //
        strcpy (cpReturnedString, "\n");                // Use strcpy () and strcat () or the program will return only 1 of the lines containing the searched request.
        while (fgets(caLineStorage, sizeof (caLineStorage), fpFilePointer) != NULL)// While get 1 line as string and the end of the file is not reached.
            if (strstr(caLineStorage, cpStringInput) != NULL)// Compare if the string line contains the string from user input.
              strcat(cpReturnedString, caLineStorage);  // strcat () can only be used after using strcpy () with the same string first.
    
        fclose(fpFilePointer);                          // All files must be closed.
    
        return 0;                                       // Error code for all is OK.
    }
    int AddRequestToDatabase (char *cpStringInput)
    {
                                                        // Local variables //
        unsigned int i = 0;                             // Counter.
        int iSpacesFlag = 1;                            // While the spaces are present flag is 1, the program will ask the user for a new input without spaces.
        //char caStringInput [1024];                    // The request to be searched for will be stored in this character array.
        FILE *fpFilePointer;                            // Declare a pointer to a type file.
    
        fpFilePointer=fopen("database.cfc", "a");       // Open the file for append the end of the file.
                                                        // Error handling for file opening.
        if(fpFilePointer == NULL) {                     // If the file can not be opened.
            //puts ("Unable to open file!\n");          // Print error text.
            SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Unable to open file!\n");// Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
            return(1);                                    // Exit with error code 1.
        }
                                                        // User input //
        //puts ("This program adds a request and response to the database.\n");// Print string.
        if (iSpacesFlag == 1){                          // While there are present spaces in the user input.
        //puts ("1. Please enter the request and response to add in the database and don't use spaces!\n");// Print string.
        //gets (caStringInput);                           // Record the user input into the character array.
    
                                                        // Error handling for user input.
        for (i = 0; cpStringInput [i] != '\0'; i++)     // Scan the character array from the user input until we reach '\0'.
            if (cpStringInput [i] == ' ') {             // If there is a space in the user input(its a not allowed character).
                iSpacesFlag = 1;
            }
            else iSpacesFlag = 0;
        }
        strcat (cpStringInput, "\n");                   // If there is no error opening the file, write the character array string in the file.
        fputs (cpStringInput, fpFilePointer);
    
        fclose(fpFilePointer);                           // All files must be closed or the OS will not update them and no writing will be performed.
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Short, Self Contained, Correct Example
    Before you next decide to dump 500 lines of code on us and ask us to play hunt the wumpus.

    > while (fscanf(fpFilePointer, "%s", cpMediafileName) == 1)
    This will fail miserably on the first filename with a space.

    You already use \n as a much less likely to appear in a filename anyway, so use that.

    Your mass of meaningless global variables isn't helping.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Sorry, I did not mean to dump a truck on you. It was suppose to be very well organized and a big help to someone else. I can't edit the post, or I would have. Please delete that post.

    Here is the clean example, despite that I understand the space is the problem:

    Problematic part of the code:
    Code:
    while (fscanf(fpFilePointer, "%s", cpMediafileName) == 1) // 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);
    }

    Full function with the problematic part:
    Code:
    /************************************** Plays most music/ video files **********************************************/
    // In order for the function to work, you must download and add Winmm.dll to Build options -> linker settings.
    // You must also add -lwinmm to Settings -> Compiler -> Linker settings -> Other linker options.
     
    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.
    }
     
    SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"DEBUG: Before while cycle.");               //  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) == 1) // 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.
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why not use fgets instead and just remove the trailing newline?
    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

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    I tried and tried, but it does not work:

    Expected behaviour:
    Since there is no code to wait for the mp4 to finish, all mp4s will be loaded and only the last from the file will be played.

    Measured behaviour:
    No mp4 file is played. If the file path and name is added manually into caTemporaryVariable9, then the file plays.

    Problematic part of the code:
    Code:
    while (fgets(cpMediafileName, uiSize = sizeof(cpMediafileName), fpFilePointer) != NULL)    // Until we reach the end of the file, record every row in cpMediafileName.
    {
                                                        // Check if the file is a media file, by checking its extension.
    
        if (strstr (cpMediafileName, ".mp3") ||
            strstr (cpMediafileName, ".mp4") ||
            strstr (cpMediafileName, ".avi") ||
            strstr (cpMediafileName, ".wmv") ||
            strstr (cpMediafileName, ".ogg") ||
            strstr (cpMediafileName, ".mpg") ||
            strstr (cpMediafileName, ".mpeg") ||
            strstr (cpMediafileName, ".webm") ||
            strstr (cpMediafileName, ".mov"))
            iFileIsAMediaFlag = 1;                     // Set the flag that the file is a media file.
        else
            iFileIsAMediaFlag = 0;                         // Clear the flag that the file is a media file.
    
        if (iFileIsAMediaFlag == 1)                     // If the file is a media file of the allowed types.
        {
        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);// Add the file name and path on the place of %s.
                                          // 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);// Load the file to be played in the audio buffer.
        mciSendString("play mp3", NULL, 0, NULL);        // Play the audio file.
        }
        else                        // if the file is not a media file of the allowed types.
        {
          SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorNotSupportedFile); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
        }
    }
    Full function:
    Code:
    /************************************** Plays most music/ video files **********************************************/
    // In order for the function to work, you must download and add Winmm.dll to Build options -> linker settings.
    // You must also add -lwinmm to Settings -> Compiler -> Linker settings -> Other linker options.
    
    int iPlaySound ()
    {
                                          // Local variables and initialization //
    
                                          // User input and initial warnings //
    
                                                       // the iScanDir () function.
    unsigned int uiSize = 0;                           // Holds the size of a string, to test if its a media file.
    int iFileIsAMediaFlag = 0;                           // Holds the size of a string, to test if its a media file.
    char *cpErrorText = "DEBUG: Can not open the file in iPlaySound ().";
    char *cpErrorNotSupportedFile = "The file is not a media file of the allowed types.";// Gives an error that the media file is not supported.
    FILE *fpFilePointer;
    char *cpFileOpenMode = "rb";          // Open the file for binary reading.
    char *cpFilePathName = "FilesList.cfc";// The file is in the local directory, so the path is not necessary.
    char *cpMediafileName;                  // Buffer to store 1 line for 1 file path and name from "FilesList.cfc".
    
    fpFilePointer = fopen (cpFilePathName, 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.
    }
    
    SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"DEBUG: Before while cycle.");               // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
    
    while (fgets(cpMediafileName, uiSize = sizeof(cpMediafileName), fpFilePointer) != NULL)    // Until we reach the end of the file, record every row in cpMediafileName.
    {
                                                        // Check if the file is a media file, by checking its extension.
    
        if (strstr (cpMediafileName, ".mp3") ||
            strstr (cpMediafileName, ".mp4") ||
            strstr (cpMediafileName, ".avi") ||
            strstr (cpMediafileName, ".wmv") ||
            strstr (cpMediafileName, ".ogg") ||
            strstr (cpMediafileName, ".mpg") ||
            strstr (cpMediafileName, ".mpeg") ||
            strstr (cpMediafileName, ".webm") ||
            strstr (cpMediafileName, ".mov"))
            iFileIsAMediaFlag = 1;                     // Set the flag that the file is a media file.
        else
            iFileIsAMediaFlag = 0;                         // Clear the flag that the file is a media file.
    
        if (iFileIsAMediaFlag == 1)                     // If the file is a media file of the allowed types.
        {
        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);// Add the file name and path on the place of %s.
                                          // 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);// Load the file to be played in the audio buffer.
        mciSendString("play mp3", NULL, 0, NULL);        // Play the audio file.
        }
        else                        // if the file is not a media file of the allowed types.
        {
          SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorNotSupportedFile); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
        }
    }
    
    fclose (fpFilePointer);                                 // All files must be closed.
    
    return 0;                                               // Status code for everything is OK.
    }
    Last edited by ArakelTheDragon; 04-25-2020 at 02:33 AM. Reason: Add information. Visual improvement.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (fgets(cpMediafileName, uiSize = sizeof(cpMediafileName), fpFilePointer) != NULL)
    Remember that fgets() also stores the \n at the end of the line.

    > sprintf (caTemporaryVariable9, "open "%s" type mpegvideo alias mp3", cpMediafileName);
    So you should remove that \n before trying to open the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Thanks for that. By logic I should replace the last character with a terminating zero?

    Like this:
    Code:
    cpMediafileName [uiSize] = '\0';

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArakelTheDragon
    By logic I should replace the last character with a terminating zero?
    Yes, if it is a '\n'. If the file name is too long, the '\n' will not be stored in the string, so you should check for this.

    However, I note that what you're doing right now is setting uiSize to sizeof(cpMediafileName). That's not very useful. What you want to do instead is set uiSize to strlen(cpMediafileName).
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Music player software
    By Mohamed Moataz in forum Windows Programming
    Replies: 2
    Last Post: 10-18-2013, 05:08 PM
  2. Binary file scanning and reading problem
    By igor in forum C Programming
    Replies: 9
    Last Post: 08-19-2012, 07:34 PM
  3. Replies: 5
    Last Post: 11-27-2010, 12:42 PM
  4. Portable Music Player
    By CornedBee in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 04-08-2008, 03:07 AM
  5. music player
    By yhack in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2004, 07:23 PM

Tags for this Thread