Link to home
Start Free TrialLog in
Avatar of jozne
jozne

asked on

Visual Basic Write & Input

Hi.

I would like to know how can I hmm do this ->

in example, I have textfile with 5 lines, as follows
'
moi1
moi2
moi3
moi4
moi5
'

and if I would like to load specific line, like line 3 to variable, how could I do that?
or if I would like to overwrite line 5 with something else, how can I do that?
(the text in the line should not matter, it could be anything from letters to numbers)

I know command Seek and I could use that, but the length of the text changes a lot, so
I can't actually use that..

->
Seek #1, 6 <-> but all texts aren't 5 letters, some of them could be 1, some 10 :)
or if I could do hmm this to all text

->
text: hello
'program counts how many letters. If there are under 10 letters, it adds " " to the word, until
'it has "10 letters", like -> "hello" would be "hello    " or so.
(in that case I could use Seek command and make the code "short")
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Try loads your file content into an array,then from the array you can get and assign values:

Example:

Private Function ReadFileText(ByVal FileName As String) As String
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    Open FileName For Input As #Handle
        ReadFileText = Input$(LOF(Handle), Handle)
        On Error Resume Next
    Close #Handle
    Exit Function
EHandler:
    ShowErrMsg
    On Error Resume Next
    Close #Handle
End Function

Private Sub WriteFileText(ByVal FileName As String, ByVal Source As String, Optional noCrlf As Boolean = False)
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    Open FileName For Output As #Handle
        If noCrlf Then
            Print #Handle, Source;
        Else
            Print #Handle, Source
        End If
        On Error Resume Next
    Close #Handle
    Exit Sub
EHandler:
    On Error Resume Next
    Close #Handle
End Sub

then...

Dim tmp as string
dim tmpArr() as string

tmp = ReadFileText("c:\test.txt")
tmpArr = Split(tmp,vbcrlf)

'If file got 5 or more element
if ubound(tmpArr) >= 4 then
   msgbox "moi1 = " & tmpArr(0)
   msgbox "moi2 = " & tmpArr(1)
   msgbox "moi3 = " & tmpArr(2)
   msgbox "moi4 = " & tmpArr(3)
   msgbox "moi5 = " & tmpArr(4)
end if

to write to its value, just try:
tmpArr(4) = "Hello World"

then to save it back to file:

Dim newcontent as string
newcontent = tmpArr(0) & vbcrlf _
tmpArr(1) & vbcrlf _
tmpArr(2) & vbcrlf _
tmpArr(3) & vbcrlf _
tmpArr(4)

WriteFileText "C:\test.txt", newcontent

'Hope this helps
if want make all text to save in 10 char length, you can try this:

Dim newcontent as string
newcontent = tmpArr(0) & String( 10 - len(tmpArr(0) , " " ) & vbcrlf & _
tmpArr(1) & String( 10 - len(tmpArr(1) , " " ) & vbcrlf & _
tmpArr(2) & String( 10 - len(tmpArr(2) , " " ) & vbcrlf & _
tmpArr(3) & String( 10 - len(tmpArr(3) , " " ) & vbcrlf & _
tmpArr(4) & String( 10 - len(tmpArr(4) , " " )

WriteFileText "C:\test.txt", newcontent


also change the above:
Dim newcontent as string
newcontent = tmpArr(0) & vbcrlf _
tmpArr(1) & vbcrlf _
tmpArr(2) & vbcrlf _
tmpArr(3) & vbcrlf _
tmpArr(4)

to

Dim newcontent as string
newcontent = tmpArr(0) & vbcrlf & _
tmpArr(1) & vbcrlf & _
tmpArr(2) & vbcrlf & _
tmpArr(3) & vbcrlf & _
tmpArr(4)

cheers
ASKER CERTIFIED SOLUTION
Avatar of dbrckovi
dbrckovi
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jozne
jozne

ASKER

those were fast replys, thanks. I'll test them out and find the suitable one
and maybe I'll make modifications to them so they'll suit for my needs just perfect.

Thanks guys :)

I'll post here how it turns out
Avatar of jozne

ASKER

ok thanks guys I found what I was looking for.

i was going to suggest searching for occurances of vbcrlf, you grab whole lines out by finding the vbcrlf after the line you want.

say you want line 3 just call

retLine$ = FindLine(3, strText)

strText being the string or text box you loaded the text file into, then delcare this sub in the form, or make it public and use it in a module.

Private Function FindLine(lineNum As Integer, ByVal FindIn As String) As String
    TLine& = 1
    For x = 1 To Len(FindIn)
        TCrlf& = InStr(1, FindIn, vbCrLf)
        If TCrlf& = 0 Then
            TCrlf& = Len(FindIn) + 1
        End If
        If TLine& = lineNum And TCrlf& > 1 Then
            FindLine = Mid(FindIn, 1, TCrlf& - 1)
            Exit Function
        End If
        TLine& = TLine + 1
        FindIn = Mid(FindIn, TCrlf& + 2)
    Next
    FindLine = "Not Found"
End Function
Guys, why are so many people still using the MSDOS Basic channels (the # stuff) to read/write files - if you're using VB why not use the Scripting object?
Thanx