2 Attachment(s)
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:
If Dir$(App.Path & "\out.txt") <> "" = True Then
fnum = FreeFile
Open App.Path & "\out.txt" For Input As fnum
Input #fnum, livekey
Close fnum
End If
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
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)
Re: Problems Reading a Text File with chr$ in VB6
PS. welcome to the forums!
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.
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
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
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 ...
Re: Problems Reading a Text File with chr$ in VB6
Quote:
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
Re: Problems Reading a Text File with chr$ in VB6
I tried:
VB Code:
If Dir$(App.Path & "\out.txt") <> "" = True Then
fnum = FreeFile
Open App.Path & "\out.txt" For Input As fnum
'Line Input #fnum, livekey
livekey = Input(LOF(1), 1)
Close fnum
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
Re: Problems Reading a Text File with chr$ in VB6
ok.. hate that!
Use Binary / Get...
VB Code:
Open "C:\path\to\file.txt" For Binary As #1
tmp = Space(LOF(1))
Get #1, , tmp
Close #1
Re: Problems Reading a Text File with chr$ in VB6
Quote:
Originally Posted by [A51g]Static
ok.. hate that!
Use Binary / Get...
VB Code:
Open "C:\path\to\file.txt" For Binary As #1
tmp = Space(LOF(1))
Get #1, , tmp
Close #1
Okay, I did:
VB Code:
Open App.Path & "\out.txt" For Binary As #1
tmp = Space(LOF(1))
Get #1, , tmp
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
Re: Problems Reading a Text File with chr$ in VB6
Try this sampe and let me know if it works for you:
VB Code:
Private Sub Command1_Click()
'============================
Dim strFile$, strText$
Dim file_length As Long
Dim bytes() As Byte, i%
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
Const BLOCK_SIZE = 1024
strFile = "c:\temp\bank.txt"
Open strFile For Binary Access Read As #1
file_length = LOF(1)
If file_length > 0 Then
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
ReDim bytes(BLOCK_SIZE)
For block_num = 1 To num_blocks
Get #1, , bytes()
For i = 0 To UBound(bytes)
strText = strText & Chr(bytes(i))
Next i
Next block_num
If left_over > 0 Then
ReDim bytes(left_over)
Get #1, , bytes()
For i = 0 To UBound(bytes)
strText = strText & Chr(bytes(i))
Next i
End If
End If
Close #1
Debug.Print strText
End Sub
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
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. :wave:
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
Re: Problems Reading a Text File with chr$ in VB6
Bump. Any resolutions on trying to get this as multi-line?
Thanks,
Chris
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.
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
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:
Dim arLines() As String
Dim i%
arLines = Split(strText, "[CHRIS]")
For i = 0 To UBound(arLines)
'do something here
Next i