LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

GetFirstFi​le

GetFirstFi​le not workong properly.

 

Hello, after many working days and hours i can describe the problem in GetFirstFile function.

 

For explain this bug i create directory c:\try and another directory c:\try\Folder1.

 

#include <ansi_c.h>
#include <utility.h>
static char fileName[260];

 

memset(fileName,0,sizeof(fileName));
       GetFirstFile ("C:\\try\\*", 1, 1, 1, 1, 1, 1, fileName);
       printf("%s\n",fileName);

       //Here i change searching  criteria
       GetFirstFile ("C:\\try\\*", 1, 1, 1, 1, 1, 0, fileName);
       printf("%s\n",fileName);
       printf("---------------------------------------\n");

 

 

----------------------------------------------------------------

The output is

Folder1

Folder1

 

When i trying use  GetFirstFile ("C:\\try\\*", 1, 1, 1, 1, 1, 0, fileName); at start, i do not recieve anything (fileName parameter is empty). It's OK.

 

How can i solve this problem.

 

 

Best Regards

Boris

Best Regards
Boris
0 Kudos
Message 1 of 10
(3,512 Views)

Hi,

 

one possibility to solve the problem could be to use the return parameter of GetFirstFile. In case of success, it is 0, and only then you may assume the file name to be valid. If the return value is, e.g., -1, meaning that no file was found matching your search criteria, then probably the filename is not set by the function, reflecting its old value.

 

So you may try

 

if ( GetFirstFile ("C:\\try\\*", 1, 1, 1, 1, 1, 0, fileName)==0)

{

    printf("%s\n",fileName);

 

 

0 Kudos
Message 2 of 10
(3,506 Views)

Sorry, It doesn't work!!!

 

For example: Put the code in Interactive execution in CVI and run the Code.

 

#include <ansi_c.h>
#include <utility.h>
static char fileName[260];
static int res;

 

    memset(fileName,0,sizeof(fileName));
       res = GetFirstFile ("C:\\try\\*", 1, 1, 1, 1, 1, 1, fileName);
       printf("Res = %d  FileName='%s'\n",res,fileName);

       //Here i change searching  criteria
       GetFirstFile ("C:\\try\\*", 1, 1, 1, 1, 1, 0, fileName);
       printf("Res = %d  FileName='%s'\n",res,fileName); 
       printf("---------------------------------------\n");

 

 

The output is:

Res = 0  FileName='Folder1'

Res = 0  FileName='Folder1'

---------------------------------------

 

Does anybody have a solution to this bug?

 

 

Best Regards
Boris
0 Kudos
Message 3 of 10
(3,496 Views)

@568 wrote:

For example: Put the code in Interactive execution in CVI and run the Code.


 

 

That's what I actually did Smiley Wink  and indeed I obtained an output similar to yours. However, my understanding is that this is more a bug of your code because of your neglect of the return value. Parameters of a function returned to the user are valid only if the function executed properly, i.e. without error. Obviously, according to the return value of this function, not finding a file is treated as an error and hence in such cases you must not trust any result obtained from the function, i.e. the name of the first file.

 

-> Did you try and check the return value of your second function call, too?

0 Kudos
Message 4 of 10
(3,493 Views)

Ok. I understand, But res parameter mus be -1 in second calling function (when i cange a criteria to files only)?

Best Regards
Boris
0 Kudos
Message 5 of 10
(3,489 Views)

Yes, for the second of your calls of GetFirstFile I would expect a return value of -1 if no file with the specified criteria can be found.

0 Kudos
Message 6 of 10
(3,487 Views)

Yes, I would expect this to, but, it is not.

Maybe u have any solution......

 

 

 

 

Best Regards
Boris
0 Kudos
Message 7 of 10
(3,484 Views)

It is working as expected in my case. I adapted your code as follows:

 

#include <ansi_c.h>
#include <utility.h>
static char fileName[260];
static int res1, res2;

 

    memset(fileName,0,sizeof(fileName));
       res1 = GetFirstFile ("C:\\Inetpub\\*", 1, 1, 1, 1, 1, 1, fileName);
       printf("Res = %d  FileName='%s'\n",res1,fileName);

       //Here i change searching  criteria
       res2 = GetFirstFile ("C:\\Inetpub\\*", 1, 1, 1, 1, 1, 0, fileName);
       printf("Res = %d  FileName='%s'\n",res2,fileName); 
       printf("---------------------------------------\n");

 

In the given folder I have two folders, no files, the Standard I/O windows shows accordingly (and correctly, I would say):

 

boris.jpg

0 Kudos
Message 8 of 10
(3,482 Views)

It will be even more obvious if you reset your file name in between the function calls:

 

#include <utility.h>
static char fileName[260];
static int res1, res2;

 

    memset(fileName,0,sizeof(fileName));
       res1 = GetFirstFile ("C:\\Inetpub\\*", 1, 1, 1, 1, 1, 1, fileName);
       printf("Res = %d  FileName='%s'\n",res1,fileName);

       //Here i change searching  criteria
    memset(fileName,0,sizeof(fileName));
       res2 = GetFirstFile ("C:\\Inetpub\\*", 1, 1, 1, 1, 1, 0, fileName);
       printf("Res = %d  FileName='%s'\n",res2,fileName); 
       printf("---------------------------------------\n");

 

boris.jpg

Message 9 of 10
(3,481 Views)

Oh Sorry. You Right.

 

My mistake.

 

 

Thank you very much.

Best Regards
Boris
0 Kudos
Message 10 of 10
(3,478 Views)