CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Open...for input

    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.


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Open...for input

    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


  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Open...for input

    ...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.
    ...at present time, using mainly Net 4.0, Vs 2010



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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured