Thread

Question (Sdk)

Question // Sdk

1  |  

lanezeri

Aug 15, 2001, 3:14am
Visual Basic Question:

How do you open a file from the beginning, pop a message box for the
line-by-line input, then exit sub at end of file?

It's something like this..

Open File$ as Input as #1
Do Until EOF
MsgBox #1
Loop

I'm prolly totally off.. I don't know.. I'm blanking out lol..

baron

Aug 15, 2001, 4:52am
Dim LineText as String
' Error trapping here
Open App.Path & "\myfile.txt" For Input As #1
' More error trapping here
Do Until EOF(1)
Line Input #1, Linetext 'Store line in variable
MsgBox LineText 'Or whatever you want to do with the line you read
Loop
Close #1

HTH
Baron


[View Quote]

lanezeri

Aug 15, 2001, 11:19am
Thats it! You are awesome! Thanks..

[View Quote]

the derek

Aug 15, 2001, 1:50pm
for sequential file access: (since you were using
input in your code attempt)

filenumber=freefile
open filename for input as filenumber
while not eof(filenumber)
input #filenumber, [list]
wend
close filenumber


[View Quote]

lanezeri

Aug 15, 2001, 1:59pm
That has useless code in it.. :-) Baron's way works great, thanks tho.. I
would have tried this if u would have posted it b4 Baron..

[View Quote]

the derek

Aug 15, 2001, 2:06pm
well they both do the same thing this is just how
i learned to do it i forgot that you wanted to put
the entire line into a message box :/ this code
was to use certain pieces in a line...oops

[View Quote]

trekkerx

Aug 15, 2001, 8:45pm
My way is basicly the same as barons but a little diffrent.\

Public Sub MsgFile(FileName as string)
Dim temp as string

On Error Goto ErrHandel

Open FileName for Input as #1
while Not EOF(#1)
temp = #1 & vbNewLine
wend
MsgBox temp
Exit Sub

ErrHandel
temp = "Error in reading file"
close #1
End Sub

[View Quote] > well they both do the same thing this is just how
> i learned to do it i forgot that you wanted to put
> the entire line into a message box :/ this code
> was to use certain pieces in a line...oops
>
[View Quote]

grimble

Aug 16, 2001, 2:55pm
Take at leat one thing from TheDerek's code .... the use of FreeFile.

FreeFile allocates the next available (i.e. unused) file number. Worth
remembering if you're ever going to have multiple files open at the same
time, especially if its in event driven code and therefore asynchronous. You
can't have two files open as #1 at the same time.

Just a good habit to get into.

Grims.


[View Quote]

1  |  
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2024. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn