LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

fclose doesn't work right away

Hello all,

I have a debug file that I would like to be able to open up in wordpad, but word pad says the file is in use.
So... just close the file right?  Wel... only partially.  If I step through execution in the debugger, it works.  Out of the debugger it does not.

Example

debug file open...

fprintf("Closing Debug File");                                                     <--  Written to file
fclose(debug_file);
LaunchExecutable("wordpad path_to_debug_file");                 <---  Wordpad says file is in use (though not when stepping through)
debug_file=fopen(path_to_debug_file, "a");
fprintf(debug_file, "Debug file opened");                                    <--- Written to file

fclose(debug_file);
... click in explorer on the file name and wordpad opens up.

So... how do I make CVI really let go of the file?
Any ideas?

-Patrick


0 Kudos
Message 1 of 5
(4,180 Views)
Hello Patrick.

You could try calling fflush() before fclose().

I have had success with this type of operation (synchronous file I/O) using the following Windows SDK functions:
  • CreateFile(); // used to open a new OR existing file
  • WriteFile();
  • FlushFileBuffers();
  • CloseHandle();
You will need to pay careful attention to the CreateFile() parameters; refer to the SDK help.

Hope this helps.
Colin.
0 Kudos
Message 2 of 5
(4,151 Views)

It is a thread/timing issue. At the point when you launch Wordpad, the OS will perform this in a separate thread to your program. This will likely take some time, as the OS has to find and load Wordpad.exe, which then has in turn has to go to the OS to find and load the file. Meanwhile, your program is still continuing to execute. In particular, your next steps are to re-open the debug file again - this will happen almost immediately, unless you are single-stepping when clearly the elapsed times involved are very much greater. So the net result is that, although CVI has let go of the file at the first fclose(), it has grabbed it again before Wordpad has had a chance to open it.

Do you need your program to carry on immediately after launching Wordpad? If not you could solve the problem by using system(), instead of LaunchExecutable(). If you only need Wordpad to view the file, instead of edit it, you could try using Notepad instead - this seems to be able to read a file that is still open by CVI, as long as you don't try to do a save operation.

JR

0 Kudos
Message 3 of 5
(4,143 Views)
JR,

I did figure out that this was a thread timing issue.  If I do enough other things before I open the file it works.  Strangely enough though, Delay(2)  doesn't do the trick, nor does system(), nor ProcessSystemEvents().  I haven't figured out exactly what it is that I am doing that makes it work, but I am happy to have found a moderately good solution.

Thanks for you reply.
-Patrick

0 Kudos
Message 4 of 5
(4,107 Views)

Try something like this...

int i = 0;

fclose(...);

SetBreakOnLibraryErrors (0);

while(LaunchExecutable(...) < 0)
{
    Sleep(20);  // windows sdk - windows.h
    if(i++ > 100)   // problem
         break;
}

SetBreakOnLibraryErrors (1);

0 Kudos
Message 5 of 5
(4,095 Views)