LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem: DLL using fclose doesn't close file unless exit Labview completly

Hi Chris,

      Have you tried compiling a DLL that merely opens and closes the FILE - without doing anything else (especially no char-array assignments)?  When you run this, do you get correct results - that is, a list that is "number_of_elements" long?  Just curious.

Cheers.

When they give imbeciles handicap-parking, I won't have so far to walk!
0 Kudos
Message 11 of 15
(1,150 Views)
No, but I'm pretty sure that wouldn't get me any where because number_of_elements is always correct.
 
In other words, if I start LabView and run the application many times the file in question is always changing
correctly. That means the variable number_of_elements is changing correctly.
 
FYI, I'm not the direct developer of the LabView Application, and need to be weary of testing. By the way,
my team lead wants me to can large sections of my program completely because of the "fopen" problem.
 
Help!
0 Kudos
Message 12 of 15
(1,102 Views)

I talked to my system person and he said something "like" this. That it
is a caching problem. Windows has the file in cache memory. All
references to it affect the cached file. You can do fopens (NULL not
returned, and errno not set), reads, and writes, but they do not affect
the file in question on the shared drive. He went on to say thathave to
use an unbuffered option with "creat" and change all read/writes
appropriately.

Will doing a creat with an unbuffered option (his suggestion) help?

0 Kudos
Message 13 of 15
(1,097 Views)

Solved it by myself, what do you think.

SUMMARY
C and C++ file operations, by default, perform their own data caching. This caching is in addition to the disk caching done by the operating system. Under certain conditions it may be necessary to ensure your data is fully flushed to the disk. This article explains how to ensure that your data is properly flushed to the disk.

MORE INFORMATION
To flush the C runtime buffers, you need a call to fflush for files that are opened with fopen or a call to the flush function for C++ ofstream objects. Flushing the operating system's disk cache is a little more difficult; it depends on the operating system in use.

16-bit Operating Systems - MS-DOS or Windows 3.1
In MS-DOS or Windows 3.1 running Smartdrv.exe version 4.0 or later, you have two choices. You can use the _commit C runtime function or link with Commode.obj and use the fflush C runtime function.

32-bit Windows Operating Systems
In 32-bit versions of Windows, the operating system has built-in disk caching. The only way to force a file to be flushed to disk is by linking to Commode.obj.

Commode.obj is designed to affect the way the C Runtime handles files. When you link to this .obj file, a call to the C runtime function fflush also forces the operating system to flush its cache to disk, making the call to _commit unnecessary.

Christopher Lusardi

   /* Compile options needed: none

      This sample code is designed to show some of the more
      common ways to flush both the C runtime buffers and the
      operating system cache
   */

   #include <stdio.h>
   #include <ofstream.h>

   void DoSomeCFileIO();
   void DoSomeCXXFileIO();

   void main()
   {
     DoSomeCFileIO()
     DoSomeCXXFileIO();
   }

   void DoSomeCFileIO()
   {
    FILE* CFileBuf;

   // Open CFileBuf for output & perform some writes

    fflush(CFileBuf);
    _commit(_fileno(CFileBuf));

   // The call to fflush will cause the C Runtime to flush
   // the buffers associated with CFileBuf to the
   // Operating system.
   // The call to _commit will tell Smartdrv.exe to flush
   // its cache to the disk.
   // The _commit function requires a file handle, hence the
   // call to _fileno
   // _commit will only function on 16-bit operating systems.
   // On 32-bit operating systems, you need to link to Commode.obj
   }

   void DoSomeCXXFileIO()
   {
    ofstream CXXFileBuf;

   // Open CXXFileBuf & perform some writes

    CXXFileBuf.flush();
    _commit(CXXFileBuf.rdbuf()->fd());

   // The call to flush causes the C Runtime to flush
   // the buffer associated with CXXFileBuf to the operating system.
   //
   // The call to _commit tells Smartdrv.exe to flush
   // its cache to the disk.
   //
   // The _commit function requires a file handle, hence the
   // call to ofstream.rdbuf()->fd()
   // _commit will only function on 16-bit operating systesm.
   // On 32-bit operating systems, you will need to link to Commode.obj.
   }
Message 14 of 15
(1,091 Views)

So, the solution was to put a fflush () in the source code, and just link with the commode.obj file. The fopen () problem goes away. Is there a better solution?

Christohper Lusardi

 

Message 15 of 15
(1,078 Views)