 |

08-16-2009, 08:07 AM
|
|
Centurion
|
|
Join Date: Feb 2004
Posts: 145
|
|
Working with text files...
|
What I am trying to do is open a text file, and then store each line as a string variable, for assembly later, where I will add more to it, which is also not a problem...
I've given it several trys, but without much success, I searched the web, also, but couldn't come up with any solid ways, to point my head in the right direction, I probably just need a good kick..
Reading the text line by line, is no problem, but I can't figure out how to get each line into a variable, any help / Ideas.
This what i have thus far;
Code:
Dim nFileNum As Integer, sText As String, sNextLine As Variant, lLineCount As Long
Dim Z_STEP As Double
Dim Z_DEPTH As Double
Private Sub ReadFile()
Z_DEPTH = 0.5
Z_STEP = 0.02
With Form1
.Text1.Text = ""
.CommonDialog1.FilterIndex = 4
'If an error occurs(usually when Cancel is clicked)
On Error GoTo ErrorH
'If an error occurs goto ErrorH(below)...
Dim F As Integer
F = FreeFile
.CommonDialog1.CancelError = True
.CommonDialog1.Filter = "Text Files (*.tap)|*.txt| All Files (*.*)"
DialogTitle = "OPEN"
'The Open dialog box is shown...
InitDir = "c:\"
.CommonDialog1.ShowOpen
Open .CommonDialog1.FileName For Input As F
'.Text1 = Input(LOF(F), F)
lLineCount = 1
Do While Not EOF(F)
Line Input #F, sNextLine
sNextLine = sNextLine
sText = sText & sNextLine & vbCrLf
Loop
Text1.Text = sText
Close #F
ErrorH:
Common_Error 'Call the Common_Error sub-routine...
Exit Sub
End With
End Sub
Private Sub Common_Error()
'---------------------------------
'This is used to catch the error
'that occurs when Cancel is clicked,
'otherwise the program will crash.
'---------------------------------
'If error is 32755("Cancel was clicked") then
If Err.Number = 32755 Then
'Do nothing because
'nothing is required...
End If
End Sub
Private Sub Command1_Click()
ReadFile
End Sub
|
Last edited by Cerian Knight; 08-16-2009 at 01:20 PM.
Reason: replaced 'color' with 'code' tags
|

08-16-2009, 01:38 PM
|
 |
Multi-Technologist
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 3,735
|
|
I would recommend reading the entire file into a single variable and then using 'Split' to parse the variable into a dynamic array.
Code:
Dim strInputString As String
Dim astrSplitItems() As String
'Open the text file and read the entire contents into strInputString:
.open code goes here
.
.open code ends here
'change the following to 'vbLF' to also support non-Windows text files...
'but must remove vbCR first using 'Replace'
astrSplitItems = Split(strInputString, vbCRLF)
|
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
|

08-16-2009, 02:02 PM
|
|
Centurion
|
|
Join Date: Feb 2004
Posts: 145
|
|
Working with text files
|
Heres what i have so far, it's close, but i only get thwe added text one time...
I've attached a pic, to show the result, but what I need it to do, is continue the "Z" to zero.
Dim nFileNum As Integer, sText As String, sNextLine As String, lLineCount As Long
Dim Z_STEP As Double
Dim Z_DEPTH As Double
Private Sub ReadFile()
Z_DEPTH = 0.5
Z_STEP = 0.0025
With Form1
.CommonDialog1.FilterIndex = 4
'If an error occurs(usually when Cancel is clicked)
On Error GoTo ErrorH
'If an error occurs goto ErrorH(below)...
Dim F As Integer
F = FreeFile
.CommonDialog1.CancelError = True
.CommonDialog1.Filter = "Text Files (*.tap)|*.txt| All Files (*.*)"
DialogTitle = "OPEN"
'The Open dialog box is shown...
InitDir = "c:\"
.CommonDialog1.ShowOpen
Open .CommonDialog1.FileName For Input As F
LINE_COUNT = 1
Z_MOVE = -Z_DEPTH
.RichTextBox1.Text = ""
Do While Not EOF(F)
Line Input #F, sNextLine
sNextLine = sNextLine & "Z"
If InStr(LINE_COUNT, sNextLine, "Z") Then
NEWLINE = Replace(sNextLine, "Z", "" & " Z" & Z_MOVE, 1)
End If
sText = NEWLINE & vbCrLf & sText
Z_MOVE = (Z_MOVE + Z_STEP)
LINE_COUNT = LINE_COUNT + 1
Loop
RichTextBox1 = sText
Close #F
ErrorH:
Common_Error 'Call the Common_Error sub-routine...
Exit Sub
End With
End Sub
Private Sub Common_Error()
'---------------------------------
'This is used to catch the error
'that occurs when Cancel is clicked,
'otherwise the program will crash.
'---------------------------------
'If error is 32755("Cancel was clicked") then
If Err.Number = 32755 Then
'Do nothing because
'nothing is required...
End If
End Sub
Private Sub Command1_Click()
With Form1
sText = ""
End With
ReadFile
End Sub
|
|

08-16-2009, 03:46 PM
|
 |
Multi-Technologist
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 3,735
|
|
|
It looks as if the added text should continue to the EOF. Are you saying that there is more data in the input file that is not being added?
Also, please use CODE tags around your code... Thanks.
|
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
|

08-16-2009, 05:25 PM
|
|
Centurion
|
|
Join Date: Feb 2004
Posts: 145
|
|
No, and the last bit of code i posted is longer than necesarry, the same result can be acheived with less code, see below, but i'm playing with it to try to make it work.
Ultimately, i need to read the file, and store each line, as a seperate variable, but i can't quite seem to get it. Here i've been messing with it, in order to see what i could do, but so far i've come up short...
The code to follow, gives me what i want, but if you look at the negative Z values, in the pic i posted, as they are winding up to zero, they need to get to zero. This is simply done by running the loop, and adding the Z values, to the end of each line from the file.
I feel the only way to accomplish this, is to do as i've stated above...
The code below is shorter, and does what is in the pic.
Code:
Private Sub ReadFile()
Z_DEPTH = 0.5
Z_STEP = 0.0025
With Form1
.CommonDialog1.FilterIndex = 4
'If an error occurs(usually when Cancel is clicked)
On Error GoTo ErrorH
'If an error occurs goto ErrorH(below)...
Dim F As Integer
F = FreeFile
.CommonDialog1.CancelError = True
.CommonDialog1.Filter = "Text Files (*.tap)|*.txt| All Files (*.*)"
DialogTitle = "OPEN"
'The Open dialog box is shown...
InitDir = "c:\"
.CommonDialog1.ShowOpen
Z_MOVE = -Z_DEPTH
Do Until (Z_MOVE) > 0
Open .CommonDialog1.FileName For Input As F
Do Until EOF(F)
Line Input #F, sNextLine
sLINE = sNextLine & " Z" & Z_MOVE & vbCrLf & sLINE & vbCrLf
Z_MOVE = (Z_MOVE + Z_STEP)
Loop
RichTextBox1 = sLINE
Loop
Close #F
ErrorH:
Common_Error 'Call the Common_Error sub-routine...
Exit Sub
End With
End Sub
|
Last edited by passel; 11-09-2009 at 07:03 PM.
|

08-16-2009, 06:39 PM
|
 |
Multi-Technologist
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 3,735
|
|
Quote:
Originally Posted by supercrewed
Ultimately, i need to read the file, and store each line, as a separate variable, but i can't quite seem to get it.
|
Earlier, I had mentioned using 'Split' (VB6 only, not available in VB5), which does just that in a few lines of code. Have you tried that approach or am I missing something that would preclude you from doing it that way? Once each line is in the array variable, you can access it by index (line number starting with line 0). If you need to modify the contents of the array variable items (once the file is read and Split) then loop through them (from LBound to UBound) and change them as you will.
|
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
|

08-16-2009, 08:14 PM
|
|
Centurion
|
|
Join Date: Feb 2004
Posts: 145
|
|
|
I have not yet, i will tomorrow, i'm somewhat burned out, but i will give it a go, Thanks...
|
|

08-18-2009, 11:55 AM
|
|
Centurion
|
|
Join Date: Feb 2004
Posts: 145
|
|
|
I tried that, I didn't seem to get it to work. The easiest way for me to get it to work, is to be able to place each line, from the text file, in to a variable. such as;
LINE1= "G01 X-0.6952 Y-0.3426"
LINE2= "G02 Y0.3426 I0.6952 J0.3426"
and so on. The length of the text file to be read will vary.
If I use the standard looping method, to read the file line by line, then the I run into trouble writing each line to a variable, by receiving an error, due to EOF being reached, either too soon, or too quicly, this is if I list the variables.
Is ther a way to use an array, and then use the array?
|
|

08-19-2009, 11:40 AM
|
 |
Multi-Technologist
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 3,735
|
|
|
I'm glad you got it, as I didn't have time to reply last night. Let us know if you have further questions.
|
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|