1

I'm currently working on a cvi application where I need to retrieve every .wav files of the current build directory. To do so in C, I'm using windows built-in function FindFirstFIle and FindNextFile in the following function :

int listingWavFileNameInDirectory( char projectDirectory[MAX_PATHNAME_LEN], int numberOfWavFile, char **ListOfWavFile)
{
    WIN32_FIND_DATA searchingFile;
    HANDLE handleFind = NULL;
    char workingPath[2048];

    sprintf(workingPath, "%s\\*.wav*", projectDirectory);

    if( (handleFind = FindFirstFile(workingPath, &searchingFile)) != INVALID_HANDLE_VALUE)
    {
        ListOfWavFile[0] = searchingFile.cFileName;
        i = 1;
        while(FindNextFile(handleFind, &searchingFile)
        { 
            ListOfWavFile[i] = searchingFile.cFileName;
            i++;
        }
        if( !FindClose(handleFind))
            return GetLastError();

        return 0;
    }
    else
    {
        return GetLastError();
    }
}

This function works fine for the first wav file ( ListOfWavFile[0] has the right string), but not for other file name which are get through FindNextFile and are include ListOfWavFile[i]. ListOfWavFile[i] is actually an empty string. I just don't understand why. This is my call to the previous functions :

GetProjectDir(projectDirectory);
numberOfWavFile = countingWavFileInDirectory(projectDirectory);
listOfWavFile = malloc(numberOfWavFile * sizeof(char *));
for(int i = 0; i < numberOfWavFile; i++)
{
    listOfWavFile[i] = malloc(256 * sizeof(char));
}
listingWavFileNameInDirectory(projectDirectory, numberOfWavFile, listOfWavFile); 

I'm on windows 7 64-bits and my application is compiled as a 64-bits application. I try to use Wow64DisableWow64FsRedirection like said in this thread, but it doesn't work for me.

Any ideas ?

Community
  • 1
  • 1
Nessy W.
  • 151
  • 2
  • 10
  • 1
    You need to `strcpy` these filenames, not just use `=`. – ooga Oct 30 '14 at 17:20
  • With few exceptions, strings (character arrays) in C cannot be assigned values using the `=` operator. As ooga has said, you must use a function. Most commonly `strcpy()` is used in scenarios like yours. – ryyker Oct 30 '14 at 18:59

2 Answers2

1

In LabWindows/CVI, you must

#include <ansi_c.h>  //header collector for ansi C libraries included in CVI environment

for the following suggestions to work...

You must use a string copy or concatenation function to assign values to strings, with few exceptions.

In C, other than at initialization, you cannot use the = operator to assign values to a char array (char *a;, char a[];).

For example, while something like:

char *a = {"this is an initialization string"};
//or
char a[] = {"this is an initialization string"};

Is perfectly ok...

This is not:

char a[80] = {0}; //okay
char b[] = {"this is a string"}; //okay still

a = b;  //Not OKAY  

Use strcpy instead:

strcpy(a, b);  //the correct way

In your code therefore, make the following changes:
(assuming that ListOfWavFile is a char ** that has been properly initialized and allocated memory)

strcpy(ListOfWavFile[0], searchingFile.cFileName);//HERE
i = 1;
while(FindNextFile(handleFind, &searchingFile)) //note the addition of the last ")"
{ 
    strcpy(ListOfWavFile[i], searchingFile.cFileName);//And HERE
    i++;
}

Turn your compile warnings up to maximum.
In CVI it looks like this (or similar, depending on version):
enter image description here

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Thanks for the C lesson, that's what append when you learn on the job ! I'll accept your answer, but I have one question left : do you have an explication on why is it working for the first time `ListOfWavFile[0] = searchingFile.cFileName;` but not in the second one ? – Nessy W. Oct 31 '14 at 08:22
  • @NessyW. - I am not sure how you have declared ListOfWavFile. Depending on how, may offer some explanation in terms of ***[undefined behavior](http://en.wikipedia.org/wiki/Undefined_behavior)***. It could have something to do with the line: `while(FindNextFile(handleFind, &searchingFile)` (you need another `)` ). Without seeing more I cannot say for sure. But turn your compile warning up to full. (See picture in edit) – ryyker Oct 31 '14 at 18:00
0

In addition to @ooga's comment about strcpy(), there is a missing ) in this line

while(FindNextFile(handleFind, &searchingFile)

And you should be range-checking i against the argument numberOfWavFile which you haven't used. OK, you found out the number of .wav files first, but that may have changed by the time you parse the folder. Assume nothing when it comes to array usage. So the above line should read

while (FindNextFile(handleFind, &searchingFile) && i < numberOfWavFile)
Weather Vane
  • 33,872
  • 7
  • 36
  • 56