02-16-2012 02:43 PM
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
02-17-2012 12:49 AM
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);
}
02-17-2012 07:20 AM
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?
02-17-2012 07:50 AM - edited 02-17-2012 07:58 AM
@568 wrote:
For example: Put the code in Interactive execution in CVI and run the Code.
That's what I actually did
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?
02-17-2012 07:58 AM
Ok. I understand, But res parameter mus be -1 in second calling function (when i cange a criteria to files only)?
02-17-2012 08:02 AM
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.
02-17-2012 08:30 AM
Yes, I would expect this to, but, it is not.
Maybe u have any solution......
02-17-2012 08:41 AM
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):
02-17-2012 08:44 AM
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");
02-17-2012 08:51 AM
Oh Sorry. You Right.
My mistake.
Thank you very much.