|
-
Aug 26th, 2005, 09:04 PM
#1
Thread Starter
Addicted Member
[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.
-
Aug 26th, 2005, 09:11 PM
#2
Re: Setting a string to a line of a txt file
With two ways to count the lines 
VB Code:
Option Explicit
Private Sub Form_Load()
Dim x As Integer, st As String
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
Open App.Path & "\to do.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ----------------- two ways to skin a cat --------------
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -------------------------------------------------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
For x = 0 To UBound(str)
st = st & str(x) & vbCrLf
Next x
MsgBox st
End Sub
str() is split by lines.
-
Aug 26th, 2005, 09:11 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 26th, 2005, 09:15 PM
#4
Thread Starter
Addicted Member
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.
-
Aug 26th, 2005, 09:20 PM
#5
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.
-
Aug 26th, 2005, 09:33 PM
#6
Thread Starter
Addicted Member
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)
-
Aug 26th, 2005, 09:35 PM
#7
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.
-
Aug 26th, 2005, 09:39 PM
#8
Thread Starter
Addicted Member
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.
-
Aug 26th, 2005, 09:48 PM
#9
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?
-
Aug 26th, 2005, 09:54 PM
#10
Thread Starter
Addicted Member
Re: Setting a string to a line of a txt file
Here it is.
VB Code:
Private Sub Command2_Click()
Dim x As Integer, st As String
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
If Dir$(App.Path & "\" & File1 & ".txt") = "" Then
MsgBox "File does not exist please check your spelling"
Else
Open App.Path & "\" & File1 & ".txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
str() = Split(strBuff, vbCrLf)
For x = 0 To UBound(str)
st = st & str(x) & vbCrLf
Next x
MsgBox st
Label1.Caption = "Lines: " & UBound(str) + 1
Do Until (i > UBound(str) + 1)
List1.AddItem str(i)
i = i + 1
Loop
End If
End Sub
Last edited by Garrett19212; Aug 26th, 2005 at 09:55 PM.
Reason: woops
-
Aug 26th, 2005, 09:58 PM
#11
Re: Setting a string to a line of a txt file
You have an extra ELSE in there.
VB Code:
Private Sub Command2_Click()
Dim x As Integer
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
If Dir$(App.Path & "\" & File1 & ".txt") = "" Then
MsgBox "File does not exist please check your spelling"
Else
Open App.Path & "\" & File1 & ".txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
str() = Split(strBuff, vbCrLf)
For x = 0 To UBound(str)
List1.AddItem str(x)
Next x
Label1.Caption = "Lines: " & UBound(str) + 1
End If
End Sub
This should be all you need. You don't need to build the string for the msgbox or the msgbox.
-
Aug 26th, 2005, 10:04 PM
#12
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|