Loading a text file one character at a time

esperknight
05-08-2003, 08:42 PM
Im curious on how to load a txt file one character at a time into an array.... i can do it in c++ but I can't think of how to do it in VB (pretty sad huh? :) ) I think it would have to do something like a buffer but... I'm not to sure. Can anyone help me please? Thanks!

Doug

YuanHao
05-08-2003, 09:02 PM
You could load the whole text and then parse it into characters, which I find most convenient :D. Not sure, thought maybe opening as binary...?

mohanakannan
05-08-2003, 09:57 PM
hi..
i have one Richtextbox and a command button on the form
and the following code....please check..



Private Sub Command1_Click()
Dim strArr() As String
Dim lCurrPos As Long
Dim lFileLen As Long
lFileLen = Len(RichTextBox1.Text)
lCurrPos = 1
While lCurrPos <= lFileLen
ReDim Preserve strArr(lCurrPos)
strArr(lCurrPos) = Mid$(RichTextBox1.Text, lCurrPos, 1)
lCurrPos = lCurrPos + 1
Wend
End Sub

Private Sub Form_Load()
RichTextBox1.FileName = App.Path & "\text.txt"
End Sub

Robse
05-08-2003, 11:08 PM
Im curious on how to load a txt file one character at a time into an array....

One way to do it:


Public Function ReadChars(sPath As String) As String()

Dim fn As Integer
Dim sInput() As String

ReDim sInput(0)
fn = FreeFile

Open sPath For Input As #fn

Do While Not EOF(fn)
ReDim Preserve sInput(UBound(sInput) + 1)
sInput(UBound(sInput)) = Input(1, fn)
Loop

Close #fn

ReadChars = sInput

End Function

MindGenius
05-08-2003, 11:49 PM
Or you could load the value into a temporary variable, put Do While IsNotEmpty(temp) use the Left$ function..., and add 1 to that value every time, until it is empty!
The code should go a bit like this:

Dim Temp As Integer
Dim Y As Integer
Temp = PUTYOURDATAHERE
Do While IsNotEmpty(Temp)
Y = Y + 1
X = (Left$(Temp), Y)
Temp = Temp - (Left$(Temp) ,1)
MsFlexGrid.AddItem(X)
Loop


P.S. I have no idea how to add an item to a grid. I just put MsFlexGrid.AddItem ( :chuckle: )
I'm a n00b, so it prob'bly won't work! I think one of you gurus'll have to change the Temp = Temp - (Left$(Temp) ,1) line...

OnErr0r
05-09-2003, 12:17 AM
It really makes no sense to read a file a single byte at a time. Load the file into a byte array, if you need to process it one byte at a time.

esperknight
05-09-2003, 04:58 AM
Wow, I didn't expect this many replys! Thanks everybody the code really helps :) OnErr0r, the reason i want to load it one byte at a time, i'm going to reverse it and make it so if the line goes pass so many characters, that before it it'll insert newline character :) Probably a really stupid way to go about it but it works right? :) Thank you everybody!

Doug

mohanakannan
05-09-2003, 07:52 PM
y don't y use StrReverse function..!!!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum