Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: DarkOne on February 27, 2004, 05:42 PM

Title: Question: "File Already Open" Error
Post by: DarkOne on February 27, 2004, 05:42 PM
I've tried countless methods to try and eliminate this problem such as using the FreeFile function to retrieve available file handles and still got this error.

I have looked over and over all my procedures for opening and closing files and I have closed every file that I originally opened. I have also tried setting procedures to load/save settings from files I am working with sequentially and I am still getting the error.

Any help would be appreciated.
Title: Re:Question: "File Already Open" Error
Post by: Tasha on February 27, 2004, 06:11 PM
DarkOne
Newbie

Right there says it all Dan :P

And i've read your post like 3 times, wtf are you talking about? -.-

EDIT: :P Nevermind I see what you're talking about. You're trying to open the file, and it says the file is all ready running, but like it doesn't appear anywhere? If that's the case just control alt delete and find the filename you're trying to open, close that and it should work.

If you're talking about the same thing I had problems with last week about opening like most of my programs like aim, winamp, starcraft, I found out it was a virus though and removed it, dunno if it's the same problem though :)
Title: Re:Question: "File Already Open" Error
Post by: Stealth on February 27, 2004, 06:22 PM
Make sure your code doesn't try to open any files that are already open within your program. ie:


Open fileX.txt for Input as #f
'do stuff
call OpenNewFile()
Close #f

'in OpenNewFile()
Open fileX.txt for input as #otherf 'this will cause the error, even though
'the file has a new FreeFile number, you have it open already in the calling procedure.
Title: Re:Question: "File Already Open" Error
Post by: Newby on February 27, 2004, 06:38 PM
Here is some coding that helps me.



Dim FNUM as Integer
FNUM = FreeFile()

Open App.Path & "\Boobies.txt" for Output As #FNUM
   Print #FNUM, "( . ) ( . )"
Close #FNUM



Try that.
Title: Re:Question: "File Already Open" Error
Post by: Dyndrilliac on February 27, 2004, 07:11 PM
Quote from: Newby on February 27, 2004, 06:38 PM
Here is some coding that helps me.



Dim FNUM as Integer
FNUM = FreeFile()

Open App.Path & "\Boobies.txt" for Output As #FNUM
   Print #FNUM, "( . ) ( . )"
Close #FNUM



Try that.

You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.
Title: Re:Question: "File Already Open" Error
Post by: Adron on February 27, 2004, 07:14 PM
Quote from: Dyndrilliac on February 27, 2004, 07:11 PM
You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.

I think it will assume the current directory, which may or may not be the same as the application directory.
Title: Re:Question: "File Already Open" Error
Post by: K on February 27, 2004, 07:37 PM
Quote from: Adron on February 27, 2004, 07:14 PM
Quote from: Dyndrilliac on February 27, 2004, 07:11 PM
You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.

I think it will assume the current directory, which may or may not be the same as the application directory.

Correct - for example, if you start a program from a shortcut, the working directory is often not the application directory.
Title: Re:Question: "File Already Open" Error
Post by: Newby on February 27, 2004, 07:53 PM
Quote from: Dyndrilliac on February 27, 2004, 07:11 PM
Quote from: Newby on February 27, 2004, 06:38 PM
Here is some coding that helps me.



Dim FNUM as Integer
FNUM = FreeFile()

Open App.Path & "\Boobies.txt" for Output As #FNUM
   Print #FNUM, "( . ) ( . )"
Close #FNUM



Try that.

You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.

Hehe, I did not know that. I've always used App.Path & Filename. Thanks! :)
Title: Re:Question: "File Already Open" Error
Post by: Spht on February 27, 2004, 10:24 PM
Quote from: Newby on February 27, 2004, 07:53 PM
Hehe, I did not know that. I've always used App.Path & Filename. Thanks! :)

You may want to read the posts following that one...
Title: Re:Question: "File Already Open" Error
Post by: DarkOne on February 27, 2004, 11:42 PM
Stealth, I have ensured that is not happening. Like I said, I even tried opening the files sequentially so one file is being opened/closed in order to prevent two files to open on the same file handle.

Also, Newby I have tried your method and still no sucess.

I'll try to figure it out, there 'must' be something I have overlooked obviously.

I just recently tried assigned unique file handle numbers to each and every file open/close operation in my program and I noticed that I always get an error on the LAST file handle. If I increase the file handle number, the same thing occurs, it says that file is already open.

Any idea?
Title: Re:Question: "File Already Open" Error
Post by: o.OV on February 28, 2004, 12:22 AM
Here is a possible scenario:

You "Open" a file.
You process a few lines..
and somewhere between "Open" and "Close"..
you have EXIT/GOTO setup that skips "Close".

that might be the cause of your problem  :-\
Check your coding for anything resembling that scenario..

You can give it a TEMPORARY fix
by placing "Close" before every "Open".

Add-On:

Give us the code where it errors at.
It would be easier for us to deduce the problem
if we had actual code to examine.
Title: Re:Question: "File Already Open" Error
Post by: Eric on February 28, 2004, 12:43 AM
Quote
You can give it a TEMPORARY fix
by placing "Close" before every "Open".
If the file was already closed it would just give you another error.
Title: Re:Question: "File Already Open" Error
Post by: DarkOne on February 28, 2004, 12:49 AM
Here I am creating files required to store user's settings and in this case for the sake of debugging, I am loading my 'LoadFilters' function, shown below this snippet.


Private Sub Form_Load()
   Open "Config.txt" For Append As #1
   Close #1
   Open "UserFilters.txt" For Append As #1
   Close #1
   Open "TalkFilters.txt" For Append As #1
   Close #1
   
   'LoadConfig
   LoadFilters
   .
   .
   .
End Sub


Here is the 'LoadFilters' function:

Public Function LoadFilters()
   Dim line As String
   
   Open "UserFilters.txt" For Input As #1
       Do While Not EOF(1)
           Input #1, line
           frmFilters.lstUsers.ListItems.Add , , line
       Loop
   Close #1
   
   Open "TalkFilters.txt" For Input As #1
       Do While Not EOF(1)
           Input #1, line
           frmFilters.lstText.ListItems.Add , , line
       Loop
   Close #1
End Function


When calling the LoadFilters function, I get an error reporting that the "file already open" on line #3 of this function, "Open "UserFilters.txt" For Input As #1".
Title: Re:Question: "File Already Open" Error
Post by: Newby on February 28, 2004, 01:26 AM
Quote from: LoRd[nK] on February 28, 2004, 12:43 AM
Quote
You can give it a TEMPORARY fix
by placing "Close" before every "Open".
If the file was already closed it would just give you another error.

No, that's untrue.
Title: Re:Question: "File Already Open" Error
Post by: o.OV on February 28, 2004, 06:21 AM
Well Dark.. the code you pasted runs fine.

I started a new project and pasted that code in
and voided out the object items..

Ran it with items in file and without items in file.

It ran fine.

"While Not" should be changed to "Until"
"Public Function LoadFilters()" should be changed to "Public Sub LoadFilters()"

Let's get this straight..
It errors at.. "Open "UserFilters.txt" For Input As #1" ?
I'll make an assumption in your actual code
you have it as:


LoadConfig

and not


'LoadConfig

If so.. then the problem is somewhere in LoadConfig.
Title: Re:Question: "File Already Open" Error
Post by: DarkOne on February 28, 2004, 12:21 PM
I continued my debugging this morning and found the problem. There was actually a problem in my SaveFilters procedure in which I was referring to a non-existent object. I have resolved this problem by ensuring that these objects are loaded.

I appreciate your help o.OV and everyone else to tried to help!
Title: Re:Question: "File Already Open" Error
Post by: Stealth on February 28, 2004, 02:09 PM
Quote from: o.OV on February 28, 2004, 06:21 AM
"While Not" should be changed to "Until"

My CS teacher disagrees. He considers "While Not" better form. Perhaps from his background in C/C++?
Title: Re:Question: "File Already Open" Error
Post by: Newby on February 28, 2004, 02:21 PM
I use Loop until EOF(1) -_-
Title: Re:Question: "File Already Open" Error
Post by: Grok on February 28, 2004, 02:24 PM
Do While EOF(FileNum) = False    'or NOT EOF(FileNum)
  '
Loop


That is the form I always use for reading files in VB.  Even when using Scripting.TextStream object.

Set TS = fso.OpenTextStream(...)
Do While TS.AtEndOfStream = False
   '
Loop


Title: Re:Question: "File Already Open" Error
Post by: DarkOne on February 28, 2004, 04:44 PM
Is there any advantage of using one over the other, other than typing less characters? Just a matter of preference?
Title: Re:Question: "File Already Open" Error
Post by: Imperceptus on February 28, 2004, 05:53 PM
Now would be an excellent opportunity for you to Sharpen up on how to debug a program.  Step Into, Step over, variable/expression/control watches and so forth.
Title: Re:Question: "File Already Open" Error
Post by: o.OV on February 28, 2004, 09:16 PM
Quote from: DarkOne on February 28, 2004, 12:21 PM
I continued my debugging this morning and found the problem. There was actually a problem in my SaveFilters procedure in which I was referring to a non-existent object. I have resolved this problem by ensuring that these objects are loaded.

I appreciate your help o.OV and everyone else to tried to help!

oh.. strange.
I don't understand object oriented programming very well. :-\
How did a non-existent object cause a file to remain open?
Title: Re:Question: "File Already Open" Error
Post by: o.OV on February 28, 2004, 10:11 PM
Quote from: Stealth on February 28, 2004, 02:09 PM
Quote from: o.OV on February 28, 2004, 06:21 AM
"While Not" should be changed to "Until"

My CS teacher disagrees. He considers "While Not" better form. Perhaps from his background in C/C++?

I did try testing this.

Note:
I use two different projects
because I found the arrangement of code
could actually make a difference in speed.
(slower/faster)
Can someone give an explanation as to why?

Don't forget to compile into an executable ~ !!

Do While Not



Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Dim T As Long, X As Long, XX As Long
Private Sub Form_Load()
   
   Unload Me
   
   XX = 100000000
   X = 0
   T = GetTickCount
   Do While Not X
       XX = XX - 1
       If XX = 0 Then X = -1
   Loop
   MsgBox "Do While Not " & GetTickCount - T

End Sub



and

Do Until



Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Dim T As Long, X As Long, XX As Long
Private Sub Form_Load()
   
   Unload Me
   
   XX = 100000000
   X = 0
   T = GetTickCount
   Do Until X
       XX = XX - 1
       If XX = 0 Then X = -1
   Loop
   MsgBox "Do Until " & GetTickCount - T

End Sub

Title: Re:Question: "File Already Open" Error
Post by: DarkOne on February 28, 2004, 11:54 PM
Quote from: o.OV on February 28, 2004, 09:16 PM
Quote from: DarkOne on February 28, 2004, 12:21 PM
I continued my debugging this morning and found the problem. There was actually a problem in my SaveFilters procedure in which I was referring to a non-existent object. I have resolved this problem by ensuring that these objects are loaded.

I appreciate your help o.OV and everyone else to tried to help!

oh.. strange.
I don't understand object oriented programming very well. :-\
How did a non-existent object cause a file to remain open?

I referred to the object while the file was open, but I don't quite understand why it didn't prompt me about the object. Odd.
Title: Re:Question: "File Already Open" Error
Post by: o.OV on February 29, 2004, 12:45 AM
Quote from: DarkOne on February 28, 2004, 11:54 PM
I referred to the object while the file was open, but I don't quite understand why it didn't prompt me about the object. Odd.

Are you using "On Error .." ?
Title: Re:Question: "File Already Open" Error
Post by: DarkOne on February 29, 2004, 03:14 AM
Nope.

If there are errors, I like to see what they are and fix them. I don't usually like to depend on "On Error Resume Next".
Title: Re:Question: "File Already Open" Error
Post by: SPY-3 on August 27, 2004, 08:47 AM
if you called a on error goto event where it opens the file then add Close#1 or w/e ur integer or number is