Small Basic Forum
close a file - Printable Version

+- Small Basic Forum (https://litdev.uk/mybb)
+-- Forum: Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=1)
+--- Forum: Standard Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=2)
+--- Thread: close a file (/showthread.php?tid=583)



close a file - francesco - 04-07-2026

closing a txt file in small basic, I found a link that said it wasn't necessary, but instead if I try to run the program 2 times it gives me an error, because the file is already in use by another task


RE: close a file - litdev - 04-08-2026

Hi,

It shouldn't be necessary in general, unless possibly if the two codes are actually in the process of a read at the same time.
I tested with a small code below and could not reproduce an issue, even when run 10+ instances of the program at the same time.

Code:
fileName = Program.Directory+"\largefile.txt"
While ("True")
  line = File.ReadLine(fileName,1)
  TextWindow.WriteLine(line)
EndWhile

Possible reasons for your issue:

1] The file is open in another non SB program.
2] You are using ReadContents on a very large file and it is actively reading it.

Please share a small sample program that shows the issue and more details, like the size (number of lines etc) of the text file you are reading, how you are running multiple versions of your SB code etc.  

i.e. Sufficient details for someone to have a good chance to reproduce the problem.  If we can't reproduce an issue then we are guessing.