Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Lots of Data in textfile problem reading each row

Posted on 2006-06-02
7
Medium Priority
?
180 Views
Last Modified: 2010-04-30
I am trying to read a textfile that has Avg. 3071 Chars. per line and Avg. 1K to 30K rows.  When i go to read the InputLine it gives me all the data at once and not by row. How can I seperate each line, upload that line to Field and then go read next line and so on... If i open the file in Wordpad It has the lines seperated with an Avg. 3071 on each row.


Public Sub Upload2Server(TxtName As String, sFileID As String)
    Dim FLen As Long
    Dim V As Byte
    Dim i As Long
    Me.Caption = "Uploading File to the Server"
   
    FLen = FileLen(TxtName)
 
    ProgressBar1.Min = 1
    ProgressBar1.Max = FLen
    ProgressBar1.Value = 1

    Dim TextLine
    Open TxtName For Input As #1   ' Open file.
    Do While Not EOF(1)   ' Loop until end of file.
        For i = 1 To FLen Step 2
            ProgressBar1.Value = i
           
            Line Input #1, TextLine   ' Read line into variable.
           
            sdb.OpenSQL_Data "SELECT * FROM Binary_Files WHERE FileID='" & sFileID & "';"
                gdRS.Fields("Test") = gdRS.Fields("Test") & vbcrlf & TextLine
                gdRS.Update
            sdb.CloseSQL_Data
           
            Exit For
        Next
    Loop
   
    Close #1
End Sub
0
Comment
Question by:thaburner
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:fostejo
ID: 16819438
thaburner,

LineInput will read up until the first CR+LF combination it bumps into - it sounds like the lines in your text file aren't 'divided' by a CR+LF, therefore LineInput is returning the whole file.

There are at least two ways to proceed:

1. Either alter the text file format to include the appropriate CR+LF markers - whether this is possible or not depends upon the source of the text file; if it's a database export, it should be pretty easy.

2. If the source text file can't be altered, then you'll need to identify something else that 'marks' the end of each appropriate line - for instance, is each line terminated by a tab character, a null or by a specific set of characters perhaps? - if so, we may be able to alter the coding appropriately.

cheers

0
 

Author Comment

by:thaburner
ID: 16819516
Well I use this code to take any file and turn it into Hex, the data im trying to read is the hex version of a file. Does this help any???

Private Sub Bin2Hex(ExeName As String, TxtName As String)
    Dim B As Byte
    Dim FLen As Long
    Dim i As Long
   
    Open ExeName For Binary As #1
    Open TxtName For Append As #2
   
    FLen = FileLen(ExeName)

    ProgressBar1.Min = 1
    ProgressBar1.Max = FLen
    ProgressBar1.Value = 1

    For i = 1 To FLen
        Get #1, i, B
        If Len(Hex(B)) = 2 Then
            Print #2, Hex(B);
        Else
            Print #2, "0" + Hex(B);
        End If
        ProgressBar1.Value = i
    Next
   
    Close #2
    Close #1
End Sub

and this code to turn it back

Private Sub Hex2Bin(TxtName As String, ExeName As String)
    Dim B(0 To 1) As Byte
    Dim FLen As Long
    Dim V As Byte
    Dim i As Long
   
    Open ExeName For Binary As #1
    Open TxtName For Binary As #2
   
    FLen = FileLen(TxtName)
 
    ProgressBar1.Min = 1
    ProgressBar1.Max = FLen
    ProgressBar1.Value = 1

    For i = 1 To FLen Step 2
        Get #2, i, B
        V = Val("&H" + Format(Chr(B(0))) + Format(Chr(B(1))))
        Put #1, , V
        ProgressBar1.Value = i
    Next
   
    Close #2
    Close #1
Unload Me
End Sub
0
 
LVL 10

Accepted Solution

by:
fostejo earned 2000 total points
ID: 16819943
thaburner,

Yes - that explains why your LineInput doesn't work - the Bin2Hex code creates a plain text file containing only the characters 0-9,A-F.    As there are no CR+LF delimiters, there are - as far as LineInput is concerned - no 'lines' as such.  

I think the 'average line length' of about 3071 chars your seeing is just an abberration introduced by WordPad I'm afraid - if you 'type' the created file at a DOS prompt you'll see that it's just one long very string of characters.

Also, at the moment your Upload2Server code appears to expect that the 'lines' in your input text file are just two characters long ("For i = 1 To FLen Step 2"), ie. each read gets just the one HEX value.

In order that you don't need to alter the Bin2Hex or Hex2Bin code, I'd suggest altering the 'Upload2Server' code so that it uses a similar method that you use in the Hex2Bin - ie. don't use LineInput, open the file in binary mode instead and 'get' the required data.

cheers,
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:thaburner
ID: 16862618
fostejo that explained alot and I think i got it working for now.... thx
0
 

Author Comment

by:thaburner
ID: 16862792
accually i do have one question if you know, i have a form that runs the code and uploads it to form and all i have visual on form is progressbar and command button to cancel action. When the code is runnin it wont let me cancel the process i cant even go do anything else in program, or if i move to another program IE. email client and want to go back and check progress bar it wont show up program just hangs. Any idea's or should i post another question...
0
 
LVL 10

Expert Comment

by:fostejo
ID: 16863822
thaburner,

Well, technically, yes you should post another question(!), but.. it sounds like you need to introduce a "DoEvents()" into your code - inside the loop is probably the appropriate place - this gives the O/S, other applications and your own app some breathing space for other things to happen on the system and is generally good practice anywhere where your application may well tie up the processor for a while.

However, note that using DoEvents in every iteration of a loop will introduce some delay into your software, so you can minimize how many times it's called with a couple of simle techniques; as examples:

'Without any API involvement:
FOR myloop=1 TO 1000000
   IF myloop MOD 1000 =0 THEN DoEvents()   ' ie. Only call DoEvents every 1000 iterations..
' main body of loop here
NEXT myloop


Alternatively, if you're happy with APIs you could also use the GetInputState API call which can quickly determine if there's any queued up mouse or keyboard activity.. ie:

Declare Function GetInputState Lib "user32" Alias "GetInputState" () As Long    ' In your declarations section.

FOR myloop=1 TO 1000000
   If GetInputState() <> 0 Then DoEvents   ' Only do the DoEvents() if there's some activity on the system..
' main body of loop here
NEXT myloop

cheers,
0
 

Author Comment

by:thaburner
ID: 16864157
that worked thx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question