Results 1 to 19 of 19

Thread: Problems Reading a Text File with chr$ in VB6

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Problems Reading a Text File with chr$ in VB6

    Okay, I have a few files for the Betabrite Messaging Sign that you see at banks and whatnot. It has code in it, which without word wrap shows all on 1 line. So it could easily be a string in VB. However, it has chr$ codes in it, so it will stop reading partway through it.

    My file reading code is simple, so that may be why:

    VB Code:
    1. If Dir$(App.Path & "\out.txt") <> "" = True Then
    2.  
    3.   fnum = FreeFile
    4.   Open App.Path & "\out.txt" For Input As fnum
    5.     Input #fnum, livekey
    6.       Close fnum
    7.  
    8. End If
    9.  
    10.     Text1.Text = livekey
    Attached are 2 examples, out.txt being the more complicated one and out2.txt being a simple test.

    The question is: How do you get VB to read the entire string flawlessly? This string has to be repeated back flawlessly for the sign.

    The second question: VB doesn't like handling this real well because of the chr$ in it, so how can it be parsed out in something a bit more digestable? I was thinking I could convert it to VB friendly characters then reparse to send to the sign when need be, but the question does remain of how to initally get all the info.

    This is a fun one I have been fighting with for 3 days, so I say good luck and hope maybe someone out there can help with this!

    Chris
    Attached Files Attached Files

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Problems Reading a Text File with chr$ in VB6

    Line Input #fnum, livekey

    that will get the entire line

    or if you need the WHOLE file at once

    livekey = Input(lof(1),1)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Problems Reading a Text File with chr$ in VB6

    PS. welcome to the forums!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Problems Reading a Text File with chr$ in VB6

    Quote Originally Posted by [A51g]Static
    Line Input #fnum, livekey

    that will get the entire line

    or if you need the WHOLE file at once

    livekey = Input(lof(1),1)
    Line Input would require Do...Loop if file contains multiple lines so you can read and parse each line individually.
    Othere than that I agree with Static's suggestion.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    Thanks for the welcome.

    That's a no-go. It's a single line, so no need to get more lines. The problem is the dreaded chr$ in there. There is a chr$13 (Return) and chr$27 (Escape). That's going to be the big problem. Need to expect terminating chr$ and how to combat that.

    Any other ideas?

    Chris

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Problems Reading a Text File with chr$ in VB6

    if it is just one line.. then use the WHOLE file method..

    that will grab all chr$ ' s as well
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Problems Reading a Text File with chr$ in VB6

    You know, your thread title and last explanations are very confusing, sorry.
    There is a function in VB Chr$() but you refer to some character as chr$, so what is it that you need to do? Do you want to exclude everything prior to the dollar sign or after or include ??? Can you tell us (based on the sample file you provided) what do you want to get out of it.
    btw, Line Input can read single line and not necessary from within the loop ...

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    You know, your thread title and last explanations are very confusing, sorry.
    There is a function in VB Chr$() but you refer to some character as chr$, so what is it that you need to do? Do you want to exclude everything prior to the dollar sign or after or include ??? Can you tell us (based on the sample file you provided) what do you want to get out of it.
    btw, Line Input can read single line and not necessary from within the loop ...
    Sorry about the confusion...

    In the text files attached are characters in ASCII character format (http://www.lookuptables.com/). In notepad you can see them as [] (boxes like that). Essentially they are the actual character for Enters, Escapes, etc... You can copy and paste it, but once you do then you lose some of the characters and then it freezes up the sign. So the only way to get it is from the out.txt itself. So that is the quandry right now, how in the world to get that code into VB and make it digestable.

    That's pretty much what we are looking at. So when VB reads it, it wants to terminate at a carriage return or escape or something of such.

    Chris

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    I tried:

    VB Code:
    1. If Dir$(App.Path & "\out.txt") <> "" = True Then
    2.  
    3.   fnum = FreeFile
    4.   Open App.Path & "\out.txt" For Input As fnum
    5.  
    6.     'Line Input #fnum, livekey
    7.    
    8.     livekey = Input(LOF(1), 1)
    9.       Close fnum
    10.  
    11. End If
    And what I got from VB was and error: Input past end of file.

    I tried Line Input and still the same, cuts off at that one part, same as before.

    Dunno, this seems to be one wacky text file.

    Chris

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Problems Reading a Text File with chr$ in VB6

    ok.. hate that!

    Use Binary / Get...

    VB Code:
    1. Open "C:\path\to\file.txt" For Binary As #1
    2.     tmp = Space(LOF(1))
    3.     Get #1, , tmp
    4. Close #1
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    Quote Originally Posted by [A51g]Static
    ok.. hate that!

    Use Binary / Get...

    VB Code:
    1. Open "C:\path\to\file.txt" For Binary As #1
    2.     tmp = Space(LOF(1))
    3.     Get #1, , tmp
    4. Close #1
    Okay, I did:

    VB Code:
    1. Open App.Path & "\out.txt" For Binary As #1
    2.     tmp = Space(LOF(1))
    3.     Get #1, , tmp
    4. Close #1

    And got the error: Run-time error '458' - Variable uses an automation type not supported in Visual Basic

    I think you are going somewhere good with this though, it is very interesting to try. See if you can get anywhere with the txt files I attached on the original.

    Chris

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Problems Reading a Text File with chr$ in VB6

    Try this sampe and let me know if it works for you:
    VB Code:
    1. Private Sub Command1_Click()
    2. '============================
    3. Dim strFile$, strText$
    4. Dim file_length As Long
    5. Dim bytes() As Byte, i%
    6. Dim num_blocks As Long
    7. Dim left_over As Long
    8. Dim block_num As Long
    9.  
    10. Const BLOCK_SIZE = 1024
    11.  
    12.     strFile = "c:\temp\bank.txt"
    13.     Open strFile For Binary Access Read As #1
    14.    
    15.         file_length = LOF(1)
    16.        
    17.         If file_length > 0 Then
    18.             num_blocks = file_length / BLOCK_SIZE
    19.             left_over = file_length Mod BLOCK_SIZE
    20.            
    21.             ReDim bytes(BLOCK_SIZE)
    22.             For block_num = 1 To num_blocks
    23.                 Get #1, , bytes()
    24.                 For i = 0 To UBound(bytes)
    25.                     strText = strText & Chr(bytes(i))
    26.                 Next i
    27.             Next block_num
    28.            
    29.             If left_over > 0 Then
    30.                 ReDim bytes(left_over)
    31.                 Get #1, , bytes()
    32.                 For i = 0 To UBound(bytes)
    33.                     strText = strText & Chr(bytes(i))
    34.                 Next i
    35.             End If
    36.         End If
    37.        
    38.     Close #1
    39.    
    40.     Debug.Print strText
    41.  
    42. End Sub

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    That was it! Wow, that was a pain in my backside but that code worked. That made things work a whole bunch better.

    Lets say I do have more than 1 of these in a TXT file (thinking towards the future), what would I do to make it read the next line or is it limited to 1 a text file?

    Thanks again!

    Chris

  14. #14
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Problems Reading a Text File with chr$ in VB6



    That sample code reads entire file by chunks of 1024 bytes and "collects" it into one easy to read string variable. So, you just have to parse that text.
    If number of characters on each line is fixed then set size of BLOCK_SIZE to be equal to the size of your line and process each chunk individually. I doubt however that line is fixed...

    Good luck with your project.

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    Okay, here is another one for you, similar but it is multilined now. So now instead of having just 1 line of text like this, now it has more than 1 line. Can this code be modified to read a single line at a time, instead of the whole file?

    Chris

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    Bump. Any resolutions on trying to get this as multi-line?

    Thanks,

    Chris

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

    Re: Problems Reading a Text File with chr$ in VB6

    I've never seen two-line scrolling marquees. Wouldn't that be very hard to read? Unless you could only see one line at a time, like on two sides of a building.

  18. #18

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    44

    Re: Problems Reading a Text File with chr$ in VB6

    Actually it doesn't show 2 lines at a time, but the program I am making stores different lines for it to show at different times. It will allow showing of items for customization and greetings, like changes in the time of day.

    So that is why I am making a text file that has more than 1 of these lines.

    Chris

  19. #19
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Problems Reading a Text File with chr$ in VB6

    Unless I'm missing something I don't really see any differences between single and multiline files - that sample I've posted should take care of it.
    Also, if your program creates these files then you may try to begin each line with some character(s) that you would recognize where new line begins so it would be much easier to parse.
    Say, you begin each line with [CHRIS], then use my original sample to get entire text and when you done use Split() function to actually split that text on your set of characters, create array and loop through array to parse each item (line):
    VB Code:
    1. Dim arLines() As String
    2. Dim i%
    3.  
    4. arLines = Split(strText, "[CHRIS]")
    5.     For i = 0 To UBound(arLines)
    6.         'do something here
    7.     Next i

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