|
| Author |
Message |
Superfreak3
Joined: 04 Oct 2007 Posts: 2
|
Posted: Mon Apr 16, 2007 12:04 pm Post subject: Is There a Way to Restart a Read? |
|
|
I'm reading through a text file to grab various values, three to be
exact. I do an oRead.OpenText(myfile) and I find my first value.
What I'm currently doing is closing the read, oRead.Close() and doing
an oRead.OpenText(myfile) to then find the second value and then the
same to find the third.
My question is, is there a way to restart the read from the top of the
file again to find the second and then again to find the third value
in the file? Or is closing and opening to find from the top the way
to go?
Any information appreciated!
THANKS!!
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
Ken Halter
Joined: 04 Oct 2007 Posts: 4150
|
Posted: Mon Apr 16, 2007 12:42 pm Post subject: Re: Is There a Way to Restart a Read? |
|
|
"Superfreak3" wrote in message @y80g2000hsf.googlegroups.com...
> I'm reading through a text file to grab various values, three to be
> exact. I do an oRead.OpenText(myfile) and I find my first value.
> What I'm currently doing is closing the read, oRead.Close() and doing
> an oRead.OpenText(myfile) to then find the second value and then the
> same to find the third.
>
> My question is, is there a way to restart the read from the top of the
> file again to find the second and then again to find the third value
> in the file? Or is closing and opening to find from the top the way
> to go?
>
> Any information appreciated!
>
> THANKS!!
I have no idea what object's you're using that expose 'OpenText' and things
like that. I always use VBs built in file I/O that's been there since the
early 1980's, is fast and fully debugged.
'=================
Private Sub Command1_Click()
Dim iFile As Integer
Dim sEachLine() As String
Dim sBuffer As String
Dim i As Integer
Dim j As Integer
'Read and display the entire file
iFile = FreeFile
Open "C:\Temp\TheFile.txt" For Input As iFile
'Place the contents in an array. Read as many times as you want without
accessing the disk again
sEachLine = Split(Input$(LOF(iFile), iFile))
Close iFile
For i = 0 To UBound(sEachLine)
Debug.Print sEachLine(i)
Next
'Read the first 3 lines 10 times
iFile = FreeFile
Open "C:\Temp\TheFile.txt" For Input As iFile
For i = 1 To 10
Debug.Print i
For j = 1 To 3
Line Input #iFile, sBuffer
Debug.Print j, sBuffer
Next
Seek #iFile, 1 'Rewind
Next
Close iFile
End Sub
'=================
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm |
|
| Back to top |
|
 |
Larry Serflaten
Joined: 04 Oct 2007 Posts: 2644
|
Posted: Mon Apr 16, 2007 4:00 pm Post subject: Re: Is There a Way to Restart a Read? |
|
|
"Superfreak3" wrote
> I'm reading through a text file to grab various values, three to be
> exact. I do an oRead.OpenText(myfile) and I find my first value.
> What I'm currently doing is closing the read, oRead.Close() and doing
> an oRead.OpenText(myfile) to then find the second value and then the
> same to find the third.
>
> My question is, is there a way to restart the read from the top of the
> file again to find the second and then again to find the third value
> in the file? Or is closing and opening to find from the top the way
> to go?
Can you just read the whole file into memory, and then go looking for
your values?
LFS |
|
| Back to top |
|
 |
Superfreak3
Joined: 04 Oct 2007 Posts: 2
|
Posted: Mon Apr 16, 2007 4:50 pm Post subject: Re: Is There a Way to Restart a Read? |
|
|
On Apr 16, 1:00 pm, "Larry Serflaten"
wrote:
> "Superfreak3" wrote
>
> > I'm reading through a text file to grab various values, three to be
> > exact. I do an oRead.OpenText(myfile) and I find my first value.
> > What I'm currently doing is closing the read, oRead.Close() and doing
> > an oRead.OpenText(myfile) to then find the second value and then the
> > same to find the third.
>
> > My question is, is there a way to restart the read from the top of the
> > file again to find the second and then again to find the third value
> > in the file? Or is closing and opening to find from the top the way
> > to go?
>
> Can you just read the whole file into memory, and then go looking for
> your values?
>
> LFS
If I knew how to do that, I guess so. I guess I could read my .ini
file into an array then traverse the array to get my values. My
utility works as it with .OpenText then .Close, .OpenText then .Close,
and the final .OpenText then .Close. |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Mon Apr 16, 2007 5:06 pm Post subject: Re: Is There a Way to Restart a Read? |
|
|
Superfreak3 wrote:
> On Apr 16, 1:00 pm, "Larry Serflaten"
> wrote:
>> "Superfreak3" wrote
>>
>>> I'm reading through a text file to grab various values, three to be
>>> exact. I do an oRead.OpenText(myfile) and I find my first value.
>>> What I'm currently doing is closing the read, oRead.Close() and doing
>>> an oRead.OpenText(myfile) to then find the second value and then the
>>> same to find the third.
>>
>>> My question is, is there a way to restart the read from the top of the
>>> file again to find the second and then again to find the third value
>>> in the file? Or is closing and opening to find from the top the way
>>> to go?
>>
>> Can you just read the whole file into memory, and then go looking for
>> your values?
>
> If I knew how to do that, I guess so. I guess I could read my .ini
> file into an array then traverse the array to get my values. My
> utility works as it with .OpenText then .Close, .OpenText then .Close,
> and the final .OpenText then .Close.
Why aren't you using the system INI functions, if that's what you're accessing?
--
..NET: It's About Trust!
http://vfred.mvps.org |
|
| Back to top |
|
 |
Greg Cadmes
Joined: 04 Oct 2007 Posts: 1
|
Posted: Wed Apr 18, 2007 8:11 pm Post subject: Re: Is There a Way to Restart a Read? |
|
|
On Apr 16, 8:04 am, "Superfreak3" wrote:
> I'm reading through a text file to grab various values, three to be
> exact. I do an oRead.OpenText(myfile) and I find my first value.
> What I'm currently doing is closing the read, oRead.Close() and doing
> an oRead.OpenText(myfile) to then find the second value and then the
> same to find the third.
>
> My question is, is there a way to restart the read from the top of the
> file again to find the second and then again to find the third value
> in the file? Or is closing and opening to find from the top the way
> to go?
>
> Any information appreciated!
>
> THANKS!!
Hi Superfreak3,
File IO is extreamly expensive. I agree with Mark. Read the file into
a buffer and traverse through looking for you values.
Greg |
|
| Back to top |
|
 |
Superfreak3
Joined: 04 Oct 2007 Posts: 1
|
Posted: Thu Apr 19, 2007 12:27 pm Post subject: Re: Is There a Way to Restart a Read? |
|
|
On Apr 16, 4:06 pm, "Karl E. Peterson" wrote:
> Superfreak3 wrote:
> > On Apr 16, 1:00 pm, "Larry Serflaten"
> > wrote:
> >> "Superfreak3" wrote
>
> >>> I'm reading through a text file to grab various values, three to be
> >>> exact. I do an oRead.OpenText(myfile) and I find my first value.
> >>> What I'm currently doing is closing the read, oRead.Close() and doing
> >>> an oRead.OpenText(myfile) to then find the second value and then the
> >>> same to find the third.
>
> >>> My question is, is there a way to restart the read from the top of the
> >>> file again to find the second and then again to find the third value
> >>> in the file? Or is closing and opening to find from the top the way
> >>> to go?
>
> >> Can you just read the whole file into memory, and then go looking for
> >> your values?
>
> > If I knew how to do that, I guess so. I guess I could read my .ini
> > file into an array then traverse the array to get my values. My
> > utility works as it with .OpenText then .Close, .OpenText then .Close,
> > and the final .OpenText then .Close.
>
> Why aren't you using the system INI functions, if that's what you're accessing?
> --
> .NET: It's About Trust!
> http://vfred.mvps.org- Hide quoted text -
>
> - Show quoted text -
Didn't know system INI functions existed as I'm a novice. Currently
I'm only programming when I need a utility to achieve something my
installation authoring tool can't accomplish. |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Thu Apr 19, 2007 2:10 pm Post subject: Re: Is There a Way to Restart a Read? |
|
|
Superfreak3 wrote:
> On Apr 16, 4:06 pm, "Karl E. Peterson" wrote:
>> Superfreak3 wrote:
>>> If I knew how to do that, I guess so. I guess I could read my .ini
>>> file into an array then traverse the array to get my values. My
>>> utility works as it with .OpenText then .Close, .OpenText then .Close,
>>> and the final .OpenText then .Close.
>>
>> Why aren't you using the system INI functions, if that's what you're accessing?
>
> Didn't know system INI functions existed as I'm a novice. Currently
> I'm only programming when I need a utility to achieve something my
> installation authoring tool can't accomplish.
Ah. Well, yeah, Windows has offered INI functions "forever" as that's been the
on-again, off-again method used to store program settings since the beginning.
Examples of usage are offered all over the 'net, but I'd have to suggest that the
method I've posted may be the best. It's actually an entirely comprehensive,
drop-in ready class module, suitable for either ClassicVB or VBA. See
http://vb.mvps.org/samples/kpIni if you're interested.
--
..NET: It's About Trust!
http://vfred.mvps.org
|
|
| Back to top |
|
 |
|
|