Solved

Reading one Char at a time

Posted on 2003-12-11
4
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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 …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

688 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