Solved

Visual Basic Write & Input

Posted on 2003-11-03
9
254 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 49

Expert Comment

by:Ryan Chong
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jozne
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jozne
Comment Utility
ok thanks guys I found what I was looking for.

0
 
LVL 3

Expert Comment

by:NBrownoh
Comment Utility
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
Comment Utility
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
Comment Utility
Thanx
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now