-
February 26th, 2009, 05:36 PM
#1
Resetting to beginning of file
Done plenty of reading this evening but still not sure how to do this.
I have an
open "xxxxxxxxxxxxxxxxxxxxxxxxx" for input as #1
command.
I am then searching the file for certain information.
Then I need to search the file again for diiferent information depending on the result of the first search , and I have to repeat this step several time's.
But trying the second & subsequent searches results in error because EOF is met , so I am having to close the file and reopen it for each search which of course adds time to the operation.
How do i simple return to beginning of file instead of open & close.
-
February 26th, 2009, 08:08 PM
#2
Re: Resetting to beginning of file
Show the code that you have tried...
-
February 26th, 2009, 08:42 PM
#3
Re: Resetting to beginning of file
OK , not too brilliant with VB , but here's the code i've used...
-------------------------------------------------------------------------------------------------------------------------
Private Sub Command1_Click()
maximumcount = 10
comd.Filter = "Select saved CSV from BS|*.doc"
comd.ShowOpen
Open "C:\Documents and Settings\John\Desktop\timecompare.doc" For Input As #3
Open "C:\Documents and Settings\John\Desktop\timecompare4.doc" For Output As #4
Print #4, "Log started at : " & Time
comd.Filter = "*.doc|*.doc"
comd.FileName = Mid(comd.FileName, 1, Len(comd.FileName) - 4) & ".doc"
Do While Not EOF(3)
Line Input #3, timetofind
Open (comd.FileName) For Input As #1
Do While Not EOF(1)
Line Input #1, mytext
mytexta = Mid(mytext, 12, 8)
mytextb = Mid(mytext, 32, 8)
If mytexta <= timetofind And mytextb >= timetofind Then
Countera = Countera + 1
End If
Loop
If Countera >= maximumcount Then maximumcount = Countera: bestdate = Left(mytext, 10): besttime = timetofind
Close #1
Print #4, Countera, timetofind
Countera = 0
Loop
Print #4, "Maximum seen today: " & maximumcount & " seen on " & bestdate & " at " & besttime
Print #4, "Log finished at : " & Time
Close #4
End
End Sub
-------------------------------------------------------------------------------------------------------------------------
Basically the file I open as #3 has a list of time's which need to be checked against file #1 , and then the result wriiten to #4. Then the second line of #3 is read again to be compared against info in #1 and results wriiten to #4 , but because #1 has already been read to EOF I have to close and reopen it everytime.
To give you an idea of the scale , the #3 has 86400 diiferent times that have to be compared against #1 , hence i am opening and closing #1 86400 times!
hope that all makes sense.
The program does work but takes about an hour to complete , I'm sure that without the constant opening and closing of the #1 file it would be a lot quicker.
-
February 26th, 2009, 11:20 PM
#4
Re: Resetting to beginning of file
You could load them all into string array() and then loop thru them. It'd be a lot quicker. It depends on how big the files are. You could definitely do it with #1
-
February 26th, 2009, 11:47 PM
#5
Re: Resetting to beginning of file
I have here a very old post -- year 2002. Its about creating an index in the memory (almost similar to dave's suggestion):
http://www.codeguru.com/forum/showth...threadid=31267
...and another one is creating an index file just in case your file is so big:
http://www.codeguru.com/forum/showthread.php?t=190807
See if it will help -- even with just a clue.
Marketing our skills - please participate in the survey and share your insights
-
-
February 27th, 2009, 04:05 AM
#6
Re: Resetting to beginning of file
I can't remember if seek is available when a file is opened for input or not. If it is then you could go back to the begining by using seek to 0. You could also close the file and open it again. That would be a bit slower but would be easy to add into the code.
The array option is the better choice if the file is not huge in size.
-
February 27th, 2009, 07:14 AM
#7
Re: Resetting to beginning of file
Yes, I'd also go for the array. Having the lines in memory will speed up the search loop considerably. This should also work for rather large files:
Code:
Private FileLines() As String
Private Sub ReadFile(FileName As String)
Dim a$, f%
f = FreeFile
Open FileName For Binary As #f
a$ = Space$(LOF(f))
Get #f, , a$
Close #f
FreeFile f
FileLines = Split(a$, vbCrLf)
End Sub
After that you adapt your loop to go
Code:
dim i&
For i = LBOund(FileLines) to UBound(FileLines)
...
Next
Which will walk through all the lines. Also for more than 100000 lines.
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
|