-
Jul 24th, 2016, 05:04 AM
#1
Thread Starter
Junior Member
How to read file that just create in same project
Hi Expert,
i had face a problem here that in the same project i am create a file name and naming as temp.txt, but after that i want to write the content in temp.txt and write in "actual.txt"
I try to use this
open "temp.txt" as output as #2
'step start
print #2, str1
print #2, str2
print #2, str3
print #2, str4
' when meet some condition and need to copy the content from "temp.txt" to "actual.txt" then i use the following command
close #2
open "temp.txt" as input #3
that is error in this step that "File already open".
may i know how can i solve this.
Thanks and Regards
Steve
-
Jul 24th, 2016, 06:36 AM
#2
Re: How to read file that just create in same project
1. 'open "temp.txt" as output as #2' syntax error
2. Not sure where temp.txt is going unless you have done a ChDir to somewhere
3. Always use FreeFile to get an open file number
Code:
Private Sub Form_Load() 'only an illustration
Dim hFile As Integer
Dim Str As String
hFile = FreeFile
Open "C:\temp.txt" For Output As hFile
Print #hFile, "Item 1"
Print #hFile, "Item 2"
Print #hFile, "Item 3"
Print #hFile, "Item 4"
Close hFile
hFile = FreeFile
Open "C:\temp.txt" For Input As hFile
Line Input #hFile, Str: Debug.Print Str
Line Input #hFile, Str: Debug.Print Str
Line Input #hFile, Str: Debug.Print Str
Line Input #hFile, Str: Debug.Print Str
Close hFile
End Sub
-
Jul 28th, 2016, 02:16 PM
#3
Re: How to read file that just create in same project
It's called pseudocode and it's a perfectly acceptable way of conveying a concept. Get over your expectations that no one ever post code that you can't copy, paste, and run as-is, with full production class error handling.
Now, as to the actual problem;
-Is it possible there's a #3 open somewhere else? Instead of specifying the number yourself, you should get an available number like mentioned in post 2;
Dim hFile As Long
hFile = FreeFile()
Open blah For whatever As #hFile
(omg more pseudocode! sorry jms!)
-Could an error have occured that results in the code jumping over the close statement?
-Is the file being placed in a directory where you have full permissions?
-
Jul 28th, 2016, 02:36 PM
#4
Re: How to read file that just create in same project
I was assuming that it might be that the close took some time, and the code was trying to open the file before it was actually free.
Perhaps if it is on a network drive, but just trying it out on a local drive I couldn't get it to fail.
Code:
Private Sub Command1_Click()
Open "c:\c\atemp.txt" For Output As #2
'step start
Print #2, "str1"
Print #2, "str2"
Print #2, "str3"
Print #2, "str4"
End Sub
Private Sub Command2_Click()
Close #2
Open "c:\c\atemp.txt" For Input As #3
Dim s As String
Input #3, s
Debug.Print s
Close #3
End Sub
-
Jul 28th, 2016, 02:46 PM
#5
Re: How to read file that just create in same project
That's a distinct possibility too passel; is anyone familiar with the lower level workings of the Close statement? Solving that seems hard... running a loop checking whether the next 'open' succeeds seems like a bad idea, and Sleep wouldn't be a good idea either since it could take a few milliseconds or many seconds depending on local or network, or if a local drive needs to spin up.
Issues like that are why I really believe in using the file i/o APIs in place of the VB i/o. When you close with CloseHandle() I'm pretty sure it won't return until it's closed. No idea if VB Close() calls CloseHandle. There's classes that wrap these APIs so they're almost as easy to use as VB's...
-
Jul 28th, 2016, 04:02 PM
#6
Re: How to read file that just create in same project
I'll throw in two-cents.
All of my work involves accessing files over a network. Most the data is placed in a database. However, there is always some data that's just placed in text files, or other file types (possibly using "Open SomeFile For Binary As iFle"). In fact, in certain areas, I open little ASCII files that serve some record locking purposes over the network.
All of these non-database files use the standard VB6 Open/Close statements (no API calls), and I've NEVER had any problem with delayed closes. Truth be told, I have no idea if a "Close iFle" hangs until the file is closed, but I've certainly never had any problems with immediately trying to re-open it a couple of code-lines later.
On a similar point, I will tell of something that does happen in a blue-moon. Every so often, a file will get "hung" in the networked file server. I have no idea how this happens, but it can report as open, when I know full well that no program is touching it. In these cases, it doesn't matter if I wait a few milliseconds, a few seconds, or a few years. It'll still report as open. The only solution I've found is to reboot the file server (which can be a HUGE pain). But again, it only happens in a blue-moon, which, according to Wikipedia, happens every 2 to 3 years.
From what steve2688 has posted, I don't think we're getting the full story. I don't see anyway to reproduce what he says is happening. I even played around with it, and can't come up with any scenario to reproduce his error. I thought maybe he was getting it open twice:
Code:
Private Sub Form_Load()
Open "test.txt" For Output As 3
Open "test.txt" For Binary As 1 ' <----- fail.
Open "test.txt" For Input As 2
End Sub
Private Sub Form_Load()
Open "test.txt" For Binary As 1
Open "test.txt" For Output As 3 ' <----- fail.
Open "test.txt" For Input As 2
End Sub
About the closest I could come to reproducing what he stated was as follows:
Code:
Private Sub Form_Load()
Open "test.txt" For Binary As 3
Open "test.txt" For Binary As 1
Close 1
Open "test.txt" For Input As 2 ' <----- fail.
End Sub
However, this is using "For Binary" and not "For Output" as he has stated. Again, I don't think we're getting the full story. I'd sure like to see some actual code.
Regards,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|