Solved

Visual Basic Write & Input

Posted on 2003-11-03
9
259 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…

734 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