Solved

Reading one Char at a time

Posted on 2003-12-11
4
382 Views
Last Modified: 2010-05-01
Is threre any function or any way to read one char at a time from a text file rather than
"Line Input # Statement" which reads one line?
0
Comment
Question by:fh2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9923297
you cant
but if you want

you can read in the string and then parse it

Mid(StrFile,5,1) this gets the 5th character

so you can loop through it also

for i = o to len(strfile) -1
      debug.print "Character " & i  & " = " & mid(strfile,i,1)
next

0
 
LVL 3

Accepted Solution

by:
Egore earned 100 total points
ID: 9923377
Use:

Dim C As String
C = Input(1, #FileNum)

The VB6 help file gives this example:

Dim MyChar
Open "TESTFILE" For Input As #1   ' Open file.
Do While Not EOF(1)   ' Loop until end of file.
   MyChar = Input(1, #1)   ' Get one character.
   Debug.Print MyChar   ' Print to the Immediate window.
Loop
Close #1   ' Close file.
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 9923922
Be aware that reading a file character by character is probably about the slowest way possible to read a file, by a large margin. If your file is even moderately sized, you are better reading it line by line, or by block or something, and then in your program reading the individual characters.

Zaphod.
0
 
LVL 3

Expert Comment

by:Egore
ID: 9924050
fh2,

Thanks for the prompt attention to the question!  I'm glad to have helped.

Z_Beeblebrox makes a very good point, though.  Using:

MyString = Input(LOF(FileNum), #FileNum)

to read in the whole file, and then parsing it like Mikal613 suggested is typically a much faster way.  But I don't know your requirements and reading it one character at a time may be the best solution for you (so that you don't have to read in the whole file before you can process it).

Best of luck to you.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

823 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