简体   繁体   English

在LabWindows CVI应用程序中调用FindNextFile时为空cFileName

[英]Empty cFileName when calling FindNextFile in a labwindows cvi application

I'm currently working on a cvi application where I need to retrieve every .wav files of the current build directory. 我目前正在使用cvi应用程序,需要检索当前构建目录的每个.wav文件。 To do so in C, I'm using windows built-in function FindFirstFIle and FindNextFile in the following function : 为此,我在以下函数中使用Windows内置函数FindFirstFIle和FindNextFile:

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]. 此函数对第一个wav文件(ListOfWavFile [0]具有正确的字符串)有效,但不适用于通过FindNextFile获取并包含ListOfWavFile [i]的其他文件名。 ListOfWavFile[i] is actually an empty string. ListOfWavFile [i]实际上是一个空字符串。 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. 我在Windows 7 64位系统上,我的应用程序编译为64位应用程序。 I try to use Wow64DisableWow64FsRedirection like said in this thread , but it doesn't work for me. 我尝试像在该线程中所说的那样使用Wow64DisableWow64FsRedirection,但它对我不起作用。

Any ideas ? 有任何想法吗 ?

In LabWindows/CVI, you must 在LabWindows / CVI中,您必须

#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[]; ). 在C中,除了在初始化时,不能使用=运算符将值分配给char数组( 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代替:

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) (假设ListOfWavFile是已正确初始化和分配内存的char **

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): 在CVI中,它看起来像这样(或类似,取决于版本):
在此处输入图片说明

In addition to @ooga's comment about strcpy() , there is a missing ) in this line 除了@ooga对strcpy()的评论,此行中还缺少)

while(FindNextFile(handleFind, &searchingFile)

And you should be range-checking i against the argument numberOfWavFile which you haven't used. 你应该范围检查i反对的论点numberOfWavFile你没有使用过。 OK, you found out the number of .wav files first, but that may have changed by the time you parse the folder. 好的,您首先发现了.wav文件的数量,但是在解析文件夹时,该数量可能已经改变。 Assume nothing when it comes to array usage. 在数组使用方面不做任何假设。 So the above line should read 所以上面的行应该读

while (FindNextFile(handleFind, &searchingFile) && i < numberOfWavFile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM