[RESOLVED] Getting specific information from a file?-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Getting specific information from a file?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    287

    Resolved [RESOLVED] Getting specific information from a file?

    I open a wave file using
    Open MyFile For Binary Access Read As #1
    The file is a *.wav file and I want to read some specific information like channels, strings, etc...

    To read how many channels the audio file contains I use:
    Dim NumChannels As Integer
    Get #1, 23, NumChannels
    MsgBox NumChannels
    I use the same formula for other kind of data, like long data type. The problem is when I try to do the same using a string.
    For example, I want to get the string RIFF, (bytes 1,2,3 and 4 because it's written in the beginning of the file). And I get a msgbox without any value.

    I get the RIFF string when I use
    Dim X$
    For N = 1 to 10
    X$ = Input (4, #1)
    MsgBox X$
    Next N
    With the RIFF no problem because it's at the beginning of the file, but if I want to get the WAVE string (bytes: 9,10,11 and 12) I've to read RIFF, (other string) and finally WAVE.

    How can I access directly to the data I want without reading all the information before that string?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,182

    Re: Getting specific information from a file?

    You can dim a string with a length of 4
    Open the file for binary
    use seek to move to the desired position
    use get to read the string

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    287

    Re: Getting specific information from a file?

    Quote Originally Posted by DataMiser View Post
    You can dim a string with a length of 4
    Open the file for binary
    use seek to move to the desired position
    use get to read the string
    Dim a string with a lenght of 4? How can I do that?

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,072

    Re: Getting specific information from a file?

    Dim someString As String * 4


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,182

    Re: Getting specific information from a file?

    Alternately you can Dim the string as usual then initialize it to 4 characters before you use the get method

    Code:
    Dim MyString as String
    MyString=Space(4)
    
    Get #Filenumber,,MyString

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    287

    Re: Getting specific information from a file?

    Thanks to all.
    Resolved.

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,072

    Re: Getting specific information from a file?

    Quote Originally Posted by DataMiser View Post
    Alternately you can Dim the string as usual then initialize it to 4 characters before you use the get method

    Code:
    Dim MyString as String
    MyString=Space(4)
    
    Get #Filenumber,,MyString
    That doesn't make the string length 4; it just gives it 4 spaces

    Dim MyString As String

    MyString = Space(4)

    MyString = "abcdefgh"

    MyString now contains "abcdefgh"

    but

    Dim MyString As String * 4

    MyString = "abcdefgh"

    MyString now contains "abcd" only


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,182

    Re: [RESOLVED] Getting specific information from a file?

    That doesn't make the string length 4; it just gives it 4 spaces
    4 spaces is a length of 4. It is not restricted to 4 though and is still a variable length string but ...

    It works for the task at hand.

    When you use Get with a string it will get the number of bytes that the string is long. So if .
    Code:
    Dim MyString As String
    Get #1,,Mystring
    It gets 0 bytes because MyString is empty
    Code:
    Dim MyString As String *4
    Get #1,,Mystring
    It will get 4 bytes because my string is defined as 4 characters
    Code:
    Dim MyString As String
    MyString=Space(4)
    Get #1,,Mystring
    It will get 4 bytes because MyString is 4 characters long

    I've did this many times in the past and it works just fine.

    You can even read the entire file into a string [assuming the file is not to big] using
    Code:
    Dim MyString As String
    MyString=Space(lof(1))
    Get #1,,Mystring

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.