Click to See Complete Forum and Search --> : Open...for input


AndyK
May 23rd, 2001, 10:46 PM
Here's an example of a code I have:

hfile = freefile
Open FileName for input as #hfile
do until eof(hfile)
input #hfile, iStr
if instr(.........) <> 0 then some other code
loop
close



Here's the question, when I find an Instr position of the character, How can I find that character's position in whole file. I could use a counter and count len(istr) for each line input, but is there any other way to get character position in the whole file. Second question is how to invert the procedure....say i know the position of the character in the file, ex. 3345, how can i get the line number where that character is located? I remember there was a CharToLinePos constant or something like that with some API (SendMessage API???) to do that, but i totally don't remember. If anyone can refresh my memory, i would greatly appreciate it. Thank You.

cksiow
May 24th, 2001, 02:51 AM
I suggest you read the whole file into a string variable, then you would be able to determine the char position in the file because the string variable is the mapping of the file itself. if you are not sure how to read the whole file into string variable, try http://vblib.virtualave.net, there is a function called ReadFileStr in vbFileIO class of the activeX DLL that do this for you.

HTH

Cimperiali
May 24th, 2001, 03:16 AM
...or (to read whole file in a variable) look for InputB function in msdn:

from msdn:
To copy an entire file to a variable, use the InputB function to copy bytes from a file to a variable. Since the InputB function returns an ANSI string, you must use the StrConv function to convert the ANSI string to a UNICODE string as follows:

LinesFromFile = StrConv(InputB(LOF(FileNum), FileNum), vbUnicode)


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.