Solved

Visual Basic Write & Input

Posted on 2003-11-03
9
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 52

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 52

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

617 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