LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I find if a file is present in my directory?

Hi.
I want to test if a file is present in my directory.
I tried whith open and fopen functions but the program stops if the file is no present.

FILE *stat;
if ((stat = fopen(fileEvenName, "r")) == NULL)
{
goto exit;
}

fileHandle = OpenFile(fileEvenimentName, VAL_READ_ONLY, VAL_OPEN_AS_IS,VAL_ASCII);

Please help me.
0 Kudos
Message 1 of 3
(3,235 Views)
Use:

Exist = FileExists (TestName, 0);

it's in the programmers toolbox.(programmer toolbox.fp) Include toolbox.h
Jattie van der Linde
Engineering Manager, Software & Automation
TEL Magnetic Solutions Ltd
Message 2 of 3
(3,235 Views)
The "program stops" because a library error was generated trying to open a non-existent file and CVI, by default, breaks on library errors.
FileExists() helps you avoid the file-not-found fopen error. But also look at SetBreakOnLibraryErrors() to avoid breaking on library errors. SetBreakOnLibraryErrors() controls how CVI deals with any library error, not just file-not-found. Be sure to check for errors in your own code if you do disable Break on Library Errors: don't just ignore them. I typically disable Break on Library Errors only on code where a Library error may be expected under some conditions (for example, trying to open a non-existant file).

// disable break on Library Errors
prevBreak = SetBreakOnLibraryErrors (0);
fpMyFile = fopen ("nofile", "r");

// restore previous state of break on Library Errors
SetBreakOnLibraryErrors (prevBreak);
if (fpMyFile != NULL)
{
// process file
// ...
fclose(fpMyFile);
}
Message 3 of 3
(3,235 Views)