Results 1 to 12 of 12

Thread: [RESOLVED]Setting a string to a line of a txt file

  1. #1

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Resolved [RESOLVED]Setting a string to a line of a txt file

    How would i set a string to a line in a txt file?

    Also how can I count how many lines there are in a txt file?
    Last edited by Garrett19212; Aug 26th, 2005 at 10:05 PM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Setting a string to a line of a txt file

    With two ways to count the lines


    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim x As Integer, st As String
    5.   Dim ff As Integer
    6.   Dim strBuff As String
    7.   Dim str() As String
    8.   ff = FreeFile
    9.   Open App.Path & "\to do.txt" For Input As #ff
    10.     strBuff = Input(LOF(ff), ff)
    11.   Close #ff
    12.   ' ----------------- two ways to skin a cat --------------
    13.   MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
    14.   ' -------------------------------------------------------
    15.   str() = Split(strBuff, vbCrLf)
    16.   MsgBox "There are " & UBound(str) + 1 & " lines in the file"
    17.   For x = 0 To UBound(str)
    18.     st = st & str(x) & vbCrLf
    19.   Next x
    20.   MsgBox st
    21.  
    22. End Sub


    str() is split by lines.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Setting a string to a line of a txt file

    Many ways for this.

    You can open the file and loop reading each line and incrementing a counter varable.
    Open the file all in one shot into an string array. Then split it and get the UBound of the array.
    '...
    '...

    Is this a large file?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Re: Setting a string to a line of a txt file

    It depends, maybe a few hundred lines or more.
    How can I set each line into a string array that would be best I think.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Setting a string to a line of a txt file

    My code above reads the whole file in, and then splits it by carriage return-line feed vbCRLF, resulting in an array str() of lines.

  6. #6

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Re: Setting a string to a line of a txt file

    Im getting an expected array error.
    i is declared as 1.
    List1.AddItem st(i)

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Setting a string to a line of a txt file

    No. st is a string that is dispayed as a msgbox. It includes the whole file and adds linefeeds to it.

    Use str(i). And use Ubound() to get the upper bound.

  8. #8

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Re: Setting a string to a line of a txt file

    How would I go about doing that, str(i) gave me subscript out of range runtime error 9, UBound(str) gave me all 2's.

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Setting a string to a line of a txt file

    Post your code. Ubound()=2 means that there were only 3 lines read in.

    Are you reading in a text file? Does it use vbCRLF for each line?

  10. #10

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Re: Setting a string to a line of a txt file

    Here it is.
    VB Code:
    1. Private Sub Command2_Click()
    2. Dim x As Integer, st As String
    3.   Dim ff As Integer
    4.   Dim strBuff As String
    5.   Dim str() As String
    6.   ff = FreeFile
    7.  
    8.   If Dir$(App.Path & "\" & File1 & ".txt") = "" Then
    9. MsgBox "File does not exist please check your spelling"
    10. Else
    11.  
    12.   Open App.Path & "\" & File1 & ".txt" For Input As #ff
    13.     strBuff = Input(LOF(ff), ff)
    14.   Close #ff
    15.   str() = Split(strBuff, vbCrLf)
    16.   For x = 0 To UBound(str)
    17.     st = st & str(x) & vbCrLf
    18.   Next x
    19.   MsgBox st
    20.  
    21.   Label1.Caption = "Lines: " & UBound(str) + 1
    22.   Do Until (i > UBound(str) + 1)
    23.   List1.AddItem str(i)
    24.   i = i + 1
    25.   Loop
    26. End If
    27. End Sub
    Last edited by Garrett19212; Aug 26th, 2005 at 09:55 PM. Reason: woops

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Setting a string to a line of a txt file

    You have an extra ELSE in there.

    VB Code:
    1. Private Sub Command2_Click()
    2.   Dim x As Integer
    3.   Dim ff As Integer
    4.   Dim strBuff As String
    5.   Dim str() As String
    6.   ff = FreeFile
    7.  
    8. If Dir$(App.Path & "\" & File1 & ".txt") = "" Then
    9.   MsgBox "File does not exist please check your spelling"
    10. Else
    11.   Open App.Path & "\" & File1 & ".txt" For Input As #ff
    12.     strBuff = Input(LOF(ff), ff)
    13.   Close #ff
    14.   str() = Split(strBuff, vbCrLf)
    15.   For x = 0 To UBound(str)
    16.     List1.AddItem str(x)
    17.   Next x
    18.   Label1.Caption = "Lines: " & UBound(str) + 1
    19. End If
    20. End Sub

    This should be all you need. You don't need to build the string for the msgbox or the msgbox.

  12. #12

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Re: Setting a string to a line of a txt file

    Thanks, you have helped me a ton.

Posting Permissions

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



Click Here to Expand Forum to Full Width