SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Apr 2008
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Problem Reading from Text file to Array

    Hi,

    This is the problem i am facing, I have a text file where i have around 65 lines of data and i need to read each of these lines and store them into an array. When i run the below script it gives me an bad length error

    Sub main
    Dim acctno as String*30
    Dim recno as Integer
    Dim msgtext as String
    Dim newline as String
    Dim Baseline() As String
    recno=1
    ReDim Baseline(0)
    Open "C:\TEMP001" For Random As #1 Len=30
    Do Until recno=11
    Get #1,recno,acctno
    Baseline(UBound(Baseline)) = acctno
    ReDim Preserve Baseline(UBound(Baseline)+1)
    recno=recno+1
    Loop
    Close #1
    End Sub

    can you kindly help me resolve this issue
    Thanks & Regards,
    Ashok Sanjeev

  2. #2
    Advanced Member Ciege's Avatar
    Join Date
    Feb 2002
    Location
    Scottsdale, AZ, USA
    Posts
    594
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Problem Reading from Text file to Array

    You have a Do Until recno=11 line. Are you ever getting a recno equal to 11? If not, it is probably trying to read beyond the end of the file.

    Try just reading each line into an array until EOF then parse the array from LBOUND to UBOUND.


    intFileNum = Freefile
    Open strFileName for Input as #intFileNum
    Do Until EOF(intFileNum)
    Line Input #intFileNum, strLineInput
    intRowCount = intRowCount + 1
    ReDim Preserve aryFile(intRowCount)
    aryFile(intRowCount) = strLineInput
    Loop
    Close #intFileNum
    If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

    Ciege...

  3. #3
    Junior Member
    Join Date
    Apr 2008
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Problem Reading from Text file to Array

    Hi Ciege,

    Thanks for providing the lead and the solution does work.Have made some modification to fit into my requirement and this is what i ended up with.......Just perfect for me

    Option Base 1 'default lower bound to use for array subscripts

    Const strBaselineFile = <filename>
    Declare Function ReadFileToArray(strFileName As String,arrayFile() As String)

    Sub Main
    Dim strBaselineContent() As String 'array to hold each line of the file read
    Dim i As Integer

    Call ReadFileToArray(strBaselineFile,strBaselineContent ())

    For i = 1 to UBound(strBaselineContent)
    SqaConsoleWrite strBaselineContent(i)
    Next

    End Sub

    Function ReadFileToArray(strFileName As String,arrayFile() As String)
    Dim strLineInput As String 'variable to contain the line read
    Dim intFileNum As Long 'variable for open file to read
    Dim intRowCount As Integer 'variable to increment the rows

    ReDim arrayFile(1) 'change the lower boudn to 1

    intFileNum = Freefile ' assign the unused file number

    Open strFileName for Input as #intFileNum 'open the file to read

    Do Until EOF(intFileNum) ' Loop until End of file
    Line Input #intFileNum, strLineInput 'read a line from the file to a string variable
    intRowCount = intRowCount + 1 'increment the rowcount
    arrayFile(UBound(arrayFile)) = strLineInput 'assign the string variable value to array
    If Not EOF(intFileNum) Then ' check if EOF not TRUE
    ReDim Preserve arrayFile(UBound(arrayFile)+1) 'Increment the array bound by preserving all existing values
    End If

    Loop

    Close #intFileNum ' Close file

    End Function
    Thanks & Regards,
    Ashok Sanjeev

  4. #4
    Advanced Member Ciege's Avatar
    Join Date
    Feb 2002
    Location
    Scottsdale, AZ, USA
    Posts
    594
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Problem Reading from Text file to Array

    Excellent. Glad I could be of assistance.
    If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

    Ciege...

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 11.54%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 11:53 AM.

Copyright BetaSoft Inc.