CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2004
    Posts
    3

    How to get byte position? [Resolved]

    Hi, everyone!
    I'm newbie of course
    Here is my problem.I want to search some text in binary file (created by me).So far, so good. But when I try to get byte position for searched string - nothings happen.Here is my code:

    Dim strFile As String
    Dim bytPos As Byte
    Dim lngTelPos As Long
    Open "example.ran" For Binary Access Read Lock Read Write As #1
    strFile = Input(LOF(1), #1)
    bytPos = InStr(1, strFile, strName)
    lngTelPos = Loc(1)
    If bytPos > 0 Then
    Get #1, lngTelPos + 1, strTel
    a = MsgBox(...)
    Else
    a = MsgBox(...)
    End If
    Close #1
    Last edited by bilidim; August 11th, 2004 at 06:28 AM.

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332
    If I understand what you intend to do correctly, then you aren't accessing the file properly. There doesn't seem to be a need to use both Input and Get like that. Here is a little example:
    Code:
    Dim A$, I&
    Open "MyFile.txt" For Binary As #1
      A = Space(LOF(1)) 'make buffer the size of the file
      Get #1, , A 'put the file contents into the string
    Close #1
    I = InStr(1, A, "Hello", 1)
    If I Then
      'string was found
    Else
      'not found
    End If
    Once you have the contents of the file in the string, you don't need to keep it open. If you want to extract a portion of it after finding the string, you can use the Mid() function.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Aug 2004
    Posts
    3
    Thanks WizBang!

    If you have time (and desire), can you look at the code below.
    Thank you in advance!
    The problem is following.If I start the program and fill strName and strTel, example.ran is fine, but during next start part of the old data are re-covered.
    (I don't know if this is of importance, but data are mixed - numbers,latin and cyrilic characters)


    Dim lngfilelength As Long
    Dim strTel As String
    Dim strName As String
    Dim intlengthName As Integer

    intlengthName = LenB(strName)
    Open "example.ran" For Binary Access Write Lock Read Write As #1
    lngfilelength = LOF(1)
    If lngfilelength = 0 Then
    lngfilelength = 1
    End If
    Put #1, lngfilelength, strName
    Put #1, intlengthName + 1, strTel
    Close #1

    Thanks again!

  4. #4
    Join Date
    Nov 2002
    Posts
    82
    Hallo bilidim,

    The seek and Loc Function may help you.

    see here for the a seek function example, and here for a Loc Function Example.
    james

  5. #5
    Join Date
    Nov 2002
    Posts
    82
    Hallo bilidim,


    here how it must be done

    Code:
    
    Dim strTel As String
    Dim strName As String
    
    Dim LngBytePosition As Long
    
    
        strName = "James"
        strTel = "001 123 456 789"
    
    Open "example.ran" For Binary Access Write Lock Read Write As #1
        
        LngBytePosition = LOF(1)
    
        If LngBytePosition = 0 Then
            LngBytePosition = 1
        Else
            LngBytePosition = LngBytePosition + 2
        End If
        
        
        Put #1, LngBytePosition, strName
        LngBytePosition = Seek(1) + 1
        
        Seek #1, LngBytePosition
        Put #1, LngBytePosition, strTel
        
    Close #1

    you can also use FileSystemObject and open the file for ForAppending
    james

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332
    If you always want to re-write the entire contents of the file, then don't use Binary (you can open for append if you need though).
    Here's an example:
    Code:
    Open "C:\test.txt" For Output As #1
      Print #1, strName; strTel;
    Close #1
    Every time it writes the file, all previous contents are lost.
    Note that this will not put any delimiter between strings, nor CrLf at the end of the line. As for the mixed character types, I think Print treats characters according to the system settings.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    Aug 2004
    Posts
    3
    Thanks for advices!

    The program is now properly working.
    Eh, in fact, there are a lot of things that must be finished, but I hope I'll do it mysef without using guru (often )

    Thanks again!

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