Solved

Visual Basic Write & Input

Posted on 2003-11-03
9
256 Views
Last Modified: 2013-11-25
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")
0
Comment
Question by:jozne
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 9676765
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
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 9676787
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
0
 
LVL 11

Accepted Solution

by:
dbrckovi earned 125 total points
ID: 9676801
Create a file with some lines. 5 or more for this example and save it to c:\test\test.txt. Then use this code to change some lines.

Private Sub Command1_Click()
   
    x = 0
    Open "c:\test\test.txt" For Input As #1
        Do Until EOF(1)
            LineCount = LineCount + 1                    'Count lines
            Line Input #1, temp
        Loop
    Close #1
       
    ReDim FileLine(LineCount) As String                  'Dimension an array to store lines
       
    Open "c:\test\test.txt" For Input As #1
        Do
            x = x + 1
            Line Input #1, FileLine(x)                   'Store the lines
        Loop Until EOF(1)
    Close #1
   
    FileLine(1) = "Changed 1"                            'Change some lines
    FileLine(4) = "Changed 4"
    FileLine(5) = "Changed 5"
   
   
    Open "c:\test\test.txt" For Output As #1            'Save the lines to file
        For x = 1 To LineCount
            Print #1, FileLine(x)
        Next x
    Close #1

End Sub
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jozne
ID: 9676839
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
0
 

Author Comment

by:jozne
ID: 9676864
ok thanks guys I found what I was looking for.

0
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9677259
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
0
 

Expert Comment

by:p_hip
ID: 9705125
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?
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 10163698
Thanx
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question