Using close function twice loses the filename

Other discussions about CSPro
Forum rules
New release: CSPro 8.0
Post Reply
Harry
Posts: 20
Joined: June 17th, 2021, 7:16 pm
Location: Costa Rica

Using close function twice loses the filename

Post by Harry »

Hi CSPRO team,
I was dealing with a bug in a big application until I found the source of the problem. When we use the close funcion, the filename of the corresponding file is changed to null (empty), if you reopen the file, the original filename comes back, but if you close the file twice and if you try to open again the file, the original filename was lost. Just to show this situation please check the following lines:

setfile(TEST_DICT,"JUSTFORTEST.csdb",create);
errmsg("Original filename=%v",filename(TEST_DICT));
close(TEST_DICT);
errmsg("Empty filename because the file is closed=%v",filename(TEST_DICT));
open(TEST_DICT);
errmsg("Original filename returns after open=%v",filename(TEST_DICT));
close(TEST_DICT);
close(TEST_DICT);
open(TEST_DICT);
errmsg("Original filename is lost after two consecutive close=%v",filename(TEST_DICT));

I can not say it is a CSPRO bug, but it is a weird behaviour.

Kind regards,
Harry
Gregory Martin
Posts: 1777
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Using close function twice loses the filename

Post by Gregory Martin »

The explanation for this is that the last filename is retained, but a dictionary is never fully closed, so there is always a current filename, which sometimes happens to be blank. What happens is:

1) First close. The old filename (a real one) is retained and the dictionary is set to data source type None, which doesn't have a filename: https://www.csprousers.org/help/CSPro/data_sources.html

2) Second close. The None data source is closed, with the filename for that source being retained as the old filename. The None data source doesn't have a filename, so that blank string is retained as the filename.

Since CSPro 7.0, we've been encouraging people to remove open/close statements from their code. Unless using text files that you are trying to have open in multiple applications at the same time, there are few reasons that you need to open or close files. Prior to CSPro 7.0, there were file locking issues that required these functions, but most of these issues are now gone.
Harry
Posts: 20
Joined: June 17th, 2021, 7:16 pm
Location: Costa Rica

Re: Using close function twice loses the filename

Post by Harry »

Thanks Gregory for the explanation.
In this case the close/open functions are necessary. The close was used before to call a batch application that uses that file.

Kind regards
Harry
Post Reply