Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading from a txt file

Status
Not open for further replies.

newbie1983

Programmer
Sep 9, 2003
52
0
0
GB
Hi i was wondering if there is an alternative of reading in a text file line by line. Below is some code that works well but not very efficient. Any Ideas?

Kind Regards,

Hinesh

Set File = ofso_OpenTextFile("c:\logs\" & Device & "_ping_status.txt")
For i = 1 To 3
DatafromFile = File.ReadLine
Next i
DatafromFile = File.ReadLine

Status = Left(DatafromFile, 5)
 
Hi Hinesh,

this is the way I normally do it:

Dim a,i as Integer

...

a=FreeFile
Open "c:\logs\" & Device & "_ping_status.txt" For Input As a
For i = 1 To 3
Line Input #a, DataFromFile
Next i

MakeItSo


Andreas Galambos
EDP / Technical Support Specialist
Bowne Global Solutions Wuppertal, Germany
(andreas.galambos@bowneglobal.de)
HP:
 
Quite simple really

open "file" for input as #1
do
input #1,getstring
debug.print getstring
loop until eof(1)
close #1
 
Look at the ReadAll method

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Hi Make it So,

Thanks Your code seems pretty good, is there any way you can include a condition such as EOF(file) so that it doesnt read past the line?

Kind Regards

Hinesh
 
Yepp,

you can simply add a line like

If EOF(a) then Exit For

[pc2]
MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
Bowne Global Solutions Wuppertal, Germany
(andreas.galambos@bowneglobal.de)
HP:
 
Hi makeItSo

what do u think of this?

a = FreeFile
Open "c:\logs\" & Device & "_ping_status.txt" For Input As a
For i = 1 To 3
If Not EOF(a) Then
Line Input #a, DatafromFile
End If
Next i

Kind regards,

Hinesh
 
What was the point of your original question <Hi i was wondering if there is an alternative of reading in a text file line by line>?
It looks like you're still going to do the same thing! < Line Input #a, DatafromFile> using an older technology

I've already given you the alternative to reading line by line

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
That's good. :)

If you want to read more than three lines, you could better use this:

Do While Not EOF(a)
Line Input #a, DatafromFile
...
Loop

But don't forget to do something with dataFromFile, else you overwrite it with each turn and you only´keep the very last line in it... ;-)

MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
Bowne Global Solutions Wuppertal, Germany
(andreas.galambos@bowneglobal.de)
HP:
 
Hi MakeItSo,

i Just need the 3 lines but i only need the 3rd line, in the vba the line

DatafromFile = File.ReadLine

usually does the trick how could this be written in vb?

kind Regards

Hinesh
 
sorry DatafromFile = File.ReadLine does work in vb lol but isnt working in this case

 
[idea] Well in that case, there's another possibility:

Do you use this text file as sort of &quot;Config&quot; file?
Then you could try this:

Write the File with this:
a = FreeFile
Open &quot;c:\logs\&quot; & Device & &quot;_ping_status.txt&quot; For Random As a
Put #a, 1, YourFirstValue
Put #a, 2, YourSecondValue
Put #a, 3, YourThirdValue
Close a

And read with this:
a = FreeFile
Open &quot;c:\logs\&quot; & Device & &quot;_ping_status.txt&quot; For Random As a
Get a, 3, DataFromFile
Close a

Then you can directly read the third (or second or whatever) line...

Does that help you?

Andy

Andreas Galambos
EDP / Technical Support Specialist
Bowne Global Solutions Wuppertal, Germany
(andreas.galambos@bowneglobal.de)
HP:
 
Hi Andy,

Not as a config file, just reading from the text file into an OLE object in vb. It doesnt seem to like the 'Put' statement i get a run-time error '75' path/file access error. I like the idea definately in the right direction. Sorry to keep bothering you im a novice/junior vb developer .

Thanks

Hinesh
 
Yepp: the PUT only works when using such a file created with &quot;As RANDOM&quot;. It then functions a bit like a text-database with markers...

You would probably then best stick with this:

a = FreeFile
Open &quot;c:\logs\&quot; & Device & &quot;_ping_status.txt&quot; For Input As a
For i = 1 To 3
If Not EOF(a) Then Line Input #a, DatafromFile
Next i

BTW: This is a Forum - you are supposed to bother others... ;-)

Cheers,
MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
Bowne Global Solutions Wuppertal, Germany
(andreas.galambos@bowneglobal.de)
HP:
 
Hi Andy,

is there a way i can interrogate the datafromfile string so i can access the 3rd line? equivalent to datafromfile = file.readline

Kind Regards

Hinesh
 
There certainly is a way.
But what do you mean by &quot;interrogate&quot;? Do you mean compare it to a certain value?
If yes, to which value exactly?
Actually I do not know the file.readline method...[3eyes]

MakeItSo

 
Hi Andy

Basically to grab a specific line from the input string datafromfile:

Open &quot;c:\logs\&quot; & Device & &quot;_ping_status.txt&quot; For Input As a
For i = 1 To 3
If Not EOF(a) Then Line Input #a, DatafromFile
Next i

btw yup the file.readline is achieved when u have an filesystemobject :)

Regards

Hinesh
 
As far as I know, this is only possible with &quot;normal&quot; input/output procedures, when you use a Text file that was created with the &quot;As Random&quot; option (like in my reference to a &quot;Config&quot; file), cause then it contains a record number.

If you open a text file &quot;For Input&quot;, a direct addressing of a specific line is not possible [sadeyes].

MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
Bowne Global Solutions Wuppertal, Germany
(andreas.galambos@bowneglobal.de)
HP:
 
Hi Andy

Thanks for the help, i got it working thanks to our friend the file system object lol

Set File = ofso_OpenTextFile(&quot;c:\logs\&quot; & Device & &quot;_ping_status.txt&quot;)
For i = 1 To 3
If Not File.AtEndOfStream Then
DatafromFile = File.ReadLine
End If
Next i
DatafromFile = File.ReadLine
MsgBox DatafromFile
Status = Left(DatafromFile, 5

Kind Regards,

Hinesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top