5 Replies - 892 Views - Last Post: 15 May 2018 - 08:21 AM Rate Topic: -----

#1 JavaThoughts   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-April 18

How to place all the codes under a single 'segment'?

Posted 15 May 2018 - 07:49 AM

Greetings, I am using VB6 and Microsoft Scripting Runtime. As for the title, I'm not sure how to explain the 'segment'. Also..sorry, this post looks long to me. I have this problem where I keep repeating this code:

Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream




I have already repeated it for like 4 times and it is said that it will take up more resources. I'm trying to just use that piece of code above for only one time for all the codes. Please ignore the commented ones, I'm just focusing on the ts=fso.OpenTextFile(txtFilePath.Text) and Do While Not ts.AtEndOfStream. My code below where the repetition occurs :

Set ts = fso.OpenTextFile(txtFilePath.Text)
    Do While Not ts.AtEndOfStream
    sInput = ts.ReadLine
'        If sInput = "MapBody{" Then
'            Do While Not ts.AtEndOfStream
'                Do
'                    sInput = ts.ReadLine
'                    sCount = sCount + 1
'                Loop Until sInput = "}"
'            Loop
'        End If
Loop
                
Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream
    pInput = ts.ReadLine
'        If pInput = "MapBody{" Then
'            ts.SkipLine
'            pInput = ts.ReadLine
'            oInput = Len(pInput)
'        End If
Loop
    
Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream
    twoInput = ts.ReadLine
'        If twoInput = "MapBody{" Then
'            Do While Not ts.AtEndOfStream
'                Do
'                twoInput = ts.ReadLine
'                        For i = 1 To Len(twoInput)
'                        atLetter = Asc(Mid(twoInput, i, 1))
'                            If atLetter = 50 Then
'                                twoCount = twoCount + 1
'                            End If
'                        Next i
'                Loop Until twoInput = "}"
'            Loop
'        End If
Loop
    
Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream
    oneInput = ts.ReadLine
'        If oneInput = "MapBody{" Then
'            Do While Not ts.AtEndOfStream
'                Do
'                oneInput = ts.ReadLine
'                        For i = 1 To Len(oneInput)
'                        aoLetter = Asc(Mid(oneInput, i, 1))
'                            If aoLetter = 49 Then
'                                oneCount = oneCount + 1
'                            End If
'                        Next i
'                Loop Until oneInput = "}"
'            Loop
'        End If
Loop
    
Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream
    
    qInput = ts.ReadLine
'        If qInput = "MapBody{" Then
'            Do While Not ts.AtEndOfStream
'            Do
'            qInput = ts.ReadLine
'                        For i = 1 To Len(qInput)
'                            tCount = tCount + 1
'                            traceInput = CStr(traceInput) & vbCrLf & tCount & " = " & Mid(qInput, i, 1)
'
'                        Next i
'            Loop Until qInput = "}"
'
'            Loop
'        End If
Loop



I commented out the rest for clarification purpose. I just want to use only a single code of this and have all the codes under it. The single code of this that I'm talking about is as below :

Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream



Is it possible? I have tried to put some of the commented code under a single 'segment' as shown below :

Set ts = fso.OpenTextFile(txtFilePath.Text)
    Do While Not ts.AtEndOfStream
'    sInput = ts.ReadLine
'        If sInput = "MapBody{" Then
'            Do While Not ts.AtEndOfStream
'                Do
'                    sInput = ts.ReadLine
'                    sCount = sCount + 1
'                Loop Until sInput = "}"
'            Loop
'        End If
'
'    pInput = ts.ReadLine
'        If pInput = "MapBody{" Then
'            ts.SkipLine
'            pInput = ts.ReadLine
'            oInput = Len(pInput)
'        End If
'
'    twoInput = ts.ReadLine
'        If twoInput = "MapBody{" Then
'            Do While Not ts.AtEndOfStream
'                Do
'                twoInput = ts.ReadLine
'                        For i = 1 To Len(twoInput)
'                        atLetter = Asc(Mid(twoInput, i, 1))
'                            If atLetter = 50 Then
'                                twoCount = twoCount + 1
'                            End If
'                        Next i
'                Loop Until twoInput = "}"
'            Loop
'        End If
        
    Loop


It seems that after the first end if, VB6 has already finish reading the textfile...
Help...

Thank you.

Regards,
Java

Is This A Good Question/Topic? 0
  • +

Replies To: How to place all the codes under a single 'segment'?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15215
  • View blog
  • Posts: 60,899
  • Joined: 12-June 08

Re: How to place all the codes under a single 'segment'?

Posted 15 May 2018 - 07:51 AM

So you want everything in a single do-while loop? Sure.. plenty of folk do that.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,185
  • Joined: 12-December 12

Re: How to place all the codes under a single 'segment'?

Posted 15 May 2018 - 08:01 AM

Please explain why you think that you need to iterate the file more than once, provide a sample of the file as well.

With a single loop, you just need to work out what to do with each line that you receive, placing it in an appropriate structure(s).

If the file is not too large then you could consider using .ReadAll and then you can parse the received data at your leisure.
Was This Post Helpful? 0
  • +
  • -

#4 JavaThoughts   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-April 18

Re: How to place all the codes under a single 'segment'?

Posted 15 May 2018 - 08:05 AM

Err...I'm not sure what kind of loop to use. I have tried to use do-while loop but I found that I can't use it like this :

Do while Set ts = fso.OpenTextFile(txtFilePath.Text)
   Do While Not ts.AtEndOfStream
   ....
   ....
   Loop
Loop



I have thought of using do-while loop before the the 'Set ts = ...', but I have no other statements?
Was This Post Helpful? 0
  • +
  • -

#5 JavaThoughts   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-April 18

Re: How to place all the codes under a single 'segment'?

Posted 15 May 2018 - 08:20 AM

View Postandrewsw, on 15 May 2018 - 08:01 AM, said:

Please explain why you think that you need to iterate the file more than once, provide a sample of the file as well.

With a single loop, you just need to work out what to do with each line that you receive, placing it in an appropriate structure(s).

If the file is not too large then you could consider using .ReadAll and then you can parse the received data at your leisure.



Iterate the file more than once, meaning that why do I keep reading it again and again? It's not that I want to iterate or read the file more than once, I can only read it once for the first end if and then the codes below can't be used since VB6 has already finished reading the file (this happens when i put all the commented codes under a single 'segment'). The sample file is a text file as below :

Mapping file = 4262970.111
Notch Side = 0
MapBody{
@...21.........
...1112........
...111111......
.112111111.....
..1111111......
....1111.......
}

I guess this is not considered large at all.

I'm trying to put everything in a single loop with loops inside (I'm trying to reduce the loops inside as well) with only one of this code block :

Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream


Placing in appropriate structure..I think there must be something wrong with my structure. I can't seems to put everything in one loop. I'll go through it again. Also, I'll try the .ReadAll.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,185
  • Joined: 12-December 12

Re: How to place all the codes under a single 'segment'?

Posted 15 May 2018 - 08:21 AM

? You already have the code working for a single loop,

Set ts = fso.OpenTextFile(txtFilePath.Text)
Do While Not ts.AtEndOfStream
    'ts.ReadLine
    'do something
Loop


As you are looping through, examining each line in sequence, decide what you need to do with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1