ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

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

Hello,

Do I have more than one option to do this correctly? I do not want to be forced to stop and start
my Labview application every time I want to close a file.

    Thank you,
    Christopher Lusardi

0 Kudos
Message 1 of 15
(4,343 Views)
Hi Christopher,
 
Can you exand on your question a little?
 
Are you calling a DLL from LabView, via Call Function Node?
 

Charlie Rodway | Principal Software Engineer | Certified TestStand Architect (CTA)

Computer Controlled Solutions Ltd | NI Silver Alliance Partner | GDevCon#1 Sponsor

0 Kudos
Message 2 of 15
(4,315 Views)

LabView is calling the DLL from a Call Function Node.


The DLL is a copy of the below code. Is there anything wrong with this code?

Christopher Lusardi

/*****************************­******************************­******************************­***/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

char file [100];

#include "my_header.h"

int create_file ()
{
  char Location [100] = "my_files/";
  FILE *Fptr;
  char box [50];
  int numalo = 0;
  int numdalo = 0;
  int mypid;
  int element;

  if ( gethostname (box, 50) < 0 )
  {
    printf ("gethostname failed\n");

    return (-1);
  }

  mypid = (int)getpid ();

  sprintf (file,"%s%s_%d",Location,box,m­ypid);

  if ( (Fptr = fopen (file,"w+")) == NULL )
  {
    printf ("fopen () failed opening file = %s\n",file);

    return (-1);
  }
  else
  {
    fprinttf (Fptr,"%d\n",Number_of_items);
    fprinttf (Fptr,"^^^^^^^^^^^^^^^^^^^^^^^­^^^\n");

    for (element = 0; element < Number_of_items;element++)
    {
      if ( vegetable [element] == 1024 )
      {
        fprintf (Fptr,"%d %d %X %d %d %X %X %X\n",
          vegetable [element],
          fruit,
          filtered_water.tomato [element],
          filtered_water.green [element],
          filtered_water.bacteria [element],
          filtered_water.organic [element],
          filtered_water.raw [element],
          filtered_water.washed [element]);
      }
      else
      {
        fprintf (Fptr,"%d %d %X %d %d %d %X %X\n",
          vegetable [element],
          fruit,
          filtered_water.tomato [element],
          filtered_water.green [element],
          filtered_water.bacteria [element],
          filtered_water.organic [element],
          filtered_water.raw [element],
          filtered_water.washed [element]);
      }
    }  
  }

  fclose (Fptr);

  return (0);

0 Kudos
Message 3 of 15
(4,304 Views)

Hi Chistopher,

Where is Number_Of_Items being initialised?

 

Charlie Rodway | Principal Software Engineer | Certified TestStand Architect (CTA)

Computer Controlled Solutions Ltd | NI Silver Alliance Partner | GDevCon#1 Sponsor

0 Kudos
Message 4 of 15
(4,305 Views)

>Where is Number_Of_Items being initialised?

Number_Of_Items is incremented/decremented based on type of request asked of a service program.

This service routine is a separate program. Many applications, running at the same speed, make the requests.

If a resource is asked for the variable is incremented. If the resource is given up it is decremented.

There are about ~20 * 2 (really copies of the 1st 20) plus the DDL's routines used to accomplish this task, 

and a large prototype routine that does the same thing. Each different routine is for a different type of request.

Christopher Lusardi

 

 

0 Kudos
Message 5 of 15
(4,298 Views)

>   fprinttf (Fptr,"%d\n",Number_of_items);
>   fprinttf (Fptr,"^^^^^^^^^^^^^^^^^^^^^^^­^^^\n");

The above code segment is a typo. It should be:

 fprintf (Fptr,"%d\n",Number_of_items);
 fprintf (Fptr,"^^^^^^^^^^^^^^^^^^^^^^^­^^^\n");

Christopher Lusardi

0 Kudos
Message 6 of 15
(4,286 Views)
Where is Number_of_items defined?  is it a static?
 

Charlie Rodway | Principal Software Engineer | Certified TestStand Architect (CTA)

Computer Controlled Solutions Ltd | NI Silver Alliance Partner | GDevCon#1 Sponsor

0 Kudos
Message 7 of 15
(4,284 Views)

> Where is Number_of_items defined?  is it a static?

It is initially defined in a dot h file for the main routine as

int Number_of_items;

and is accessed by including in many files (via another dot h file)

extern int Number_of_items;

It is set to 0 in a file that all users should use at the start of their main routines.

Christopher Lusardi

 

0 Kudos
Message 8 of 15
(4,267 Views)

O'kay, I was told that a GUI button was causing the problem. I investigated and was able to duplicate the problem

outside of the LabView environment. This means that the problem is not a DLL problem, and it is not a LabView problem.

Consequently, the problem is easier to solve, and I can do it on my own. I do not even think it is a fclose () problem.

Thank you for your mis-placed time,

Christopher Lusardi

0 Kudos
Message 9 of 15
(4,258 Views)
Hello again,
 
   I would like to reactivate this problem.
 
  The problem is I have to exit LabView completely and rerun the application to create a file if I use fp = fopen (...,"w+") as in earlier
posted code. If I do not restart LabView but I delete the file then on subsequent calls to fopen the file will not be created.
 
  What can I do to prevent exiting LabView but re-create the file on all calls to fopen or something like it?
 
 Thank you,
 Christopher Lusardi
0 Kudos
Message 10 of 15
(4,220 Views)