ThreadBoard ArchivesSite FeaturesActiveworlds SupportHistoric Archives |
Question (Sdk)
Question // SdklanezeriAug 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.. baronAug 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] the derekAug 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] lanezeriAug 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 derekAug 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] trekkerxAug 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] grimbleAug 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] |