[RESOLVED] Write\ Read data to txt file as array-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] Write\ Read data to txt file as array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    125

    Resolved [RESOLVED] Write\ Read data to txt file as array

    Hi all
    I need to write and read data to txt file . i have a 8 textbox as array index . i could write data in file but i can load data to textbox array so that each data show in own textbox. for example :afte loading data txt(0).text=txt0.all data loads in txt(0) but i want to load each data as separate textbox.
    code for wite :
    Code:
    Dim sFileText As String
     Dim iFileNo As Integer
     Dim iCount As Integer
     
     Dim sFileTextt As String
     Dim iFileNoo As Integer
     Dim iCountt As Integer
    Private Sub Command1_Click()
      
        
        iFileNo = FreeFile
            
        'open the file for reading
        Open App.Path & "\Roster1.txt" For Output As #iFileNo
        
        'read the file until we reach the end
        Do While Not iCount = 4 '30
            sFileText = Txt(iCount)
            Write #iFileNo, sFileText
            iCount = iCount + 1
        Loop
        
            Do While Not iCountt = 4 '30
            sFileTextt = Tx(iCountt)
            Write #iFileNo, sFileTextt
            iCountt = iCountt + 1
        Loop
        
        Close #iFileNo
        
        MsgBox "Written data to file."
    
    End Sub
    code for read :
    Code:
    Private Sub Command3_Click()
    Dim dText As String, dNextLine As String, lLineCount As Long
    lLineCount = 1
            Open App.Path & "\Roster1.txt" For Input As #1
            Do While Not EOF(1)
                Line Input #1, dNextLine
                dNextLine = Replace(dNextLine, Chr$(34), "")
              
                dText = dText & dNextLine '& vbCrLf
            Loop
            Txt(i).Text = dText
    End Sub
    I also attached source project
    Sorry for my poor english
    Please solve my problem
    thanks all
    Attached Files Attached Files

  2. #2
    Frenzied Member
    Join Date
    Dec 2008
    Posts
    1,045

    Re: Write\ Read data to txt file as array

    Code:
     Option Explicit
     
     'It is unclean to store two control arrays (Txt and Tx) in a single file.
     'Either have two files, or place all the textboxes into a single control array (Txt)
     
    
    Private Sub cmdWriteFile_Click()
     Dim sPathAndFile As String
     Dim sContents As String
     Dim i As Integer
        For i = 0 To Txt.UBound
            If i = 0 Then
                sContents = Txt(i).Text
            Else
                sContents = sContents & vbCrLf & Txt(i).Text
                'sContents = sContents & "LINE" & Txt(i).Text  Used to prove that the file write places undesired vbcrlf on end
            End If
        Next i
        sPathAndFile = App.Path & "\" & "Roster_Txt.txt"
        FileCreateAndSaveData sPathAndFile, sContents
        
        sContents = ""
        For i = 0 To Tx.UBound
            If i = 0 Then
                sContents = Tx(i).Text
            Else
                sContents = sContents & vbCrLf & Tx(i).Text
            End If
        Next i
        sPathAndFile = App.Path & "\" & "Roster_Tx.txt"
        FileCreateAndSaveData sPathAndFile, sContents
    End Sub
    
     
    Private Sub cmdReadFile_Click()
     Dim sPathAndFile As String
     Dim sContents As String
     Dim i As Integer
     Dim arrContents() As String
        sPathAndFile = App.Path & "\" & "Roster_Txt.txt"
        sContents = GetFileContents(sPathAndFile)
        arrContents = Split(sContents, vbCrLf)
        For i = 0 To (UBound(arrContents) - 1)   ' -1 to compensate for the File containing undesired vbcrlf on the end
            Txt(i).Text = arrContents(i)
        Next i
        
        sPathAndFile = App.Path & "\" & "Roster_Tx.txt"
        sContents = GetFileContents(sPathAndFile)
        arrContents = Split(sContents, vbCrLf)
        For i = 0 To (UBound(arrContents) - 1)   ' -1 to compensate for the File containing undesired vbcrlf on the end
            Tx(i).Text = arrContents(i)
        Next i
    End Sub
     
    Private Sub FileCreateAndSaveData(ByVal file_name As String, sData As String)
     Dim fnum As Integer
        fnum = FreeFile
        Open file_name For Output As fnum
        Print #fnum, sData
        Close fnum
    End Sub
     
     'Get File's contents, and place into string
    Private Function GetFileContents(ByVal file_name As String) As String
     Dim fnum As Integer
        fnum = FreeFile
        Open file_name For Input As fnum
        GetFileContents = Input(LOF(fnum), fnum)
        Close fnum
    End Function

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,493

    Re: Write\ Read data to txt file as array

    No need to resort to clunky "slurp and split" approaches.

    You can write data using Print# and read it back using Line Input# statements. That's why we have those statements, and the code will be less clunky, less error-prone, and you'll use less memory too. The latter doesn't matter since you only have 8 items, but it is a bad habit to use slurp and split at all.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: Write\ Read data to txt file as array

    The code you had in the first post you used Write, which is used to write data.
    In your input code you used Line Input, which is used to read line terminated strings.
    You should have been using Input, to read the data you wrote using Write. It handles removing the extra formatting character added by the Write statement to separate the pieces of data written.

    And alternative to writing and reading "data", using the Write and Input statements, is as dilettant mentioned.
    Use the Print statement to write a line of text (i.e. a string with line termination) and the Line Input statement to read the string back in (with the line termination automatically removed for you).

    The issue with your first code is you mixed the paradigms. You wrote using the "data" paradigm and read using the "string" paradigm.

    When I write data, i.e. a possible mix of strings, integers, doubles, or other types, I use the Write and Input statements since it handles all the encoding and decoding of the strings written to the file so I don't have to.
    Using Print is more for creating a text document or custom formatted string formats, generally, but can be easily used for writing "data" when you only write (Print) one data element per line so it is easily "parsed" using Line Input.
    Last edited by passel; Feb 27th, 2017 at 09:35 AM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    125

    Re: Write\ Read data to txt file as array

    thanks all for your guidance
    i could solved my issue as xml format .

  6. #6
    Frenzied Member
    Join Date
    Dec 2008
    Posts
    1,045

    Re: [RESOLVED] Write\ Read data to txt file as array

    Hi
    You should post the solution that you used

    Rob

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.