Results 1 to 5 of 5

Thread: [RESOLVED] [2005] FileStrem.Read

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [RESOLVED] [2005] FileStrem.Read

    Well i have this code:
    vb Code:
    1. Dim File As New IO.FileStream(OpenFile.FileName, IO.FileMode.Open)
    2. Dim BytesComentario(41) As Byte
    3. File.Read(BytesComentario, 11, 42)

    When I run the code OpenFile.FileName is not null, and the file that it points to exists. And this file lenth is about 150 bytes.

    Still I get this error on the third line:
    "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."

    And I really can't understand why, because i see nothing wrong.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Junior Member
    Join Date
    Jan 2008
    Posts
    24

    Re: [2005] FileStrem.Read

    Code:
    Dim File As New IO.FileStream(OpenFile.FileName, IO.FileMode.Open)
    Dim BytesComentario(41+11) As Byte
    File.Read(BytesComentario, 11, 42)

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] FileStrem.Read

    but by doing that the byte array will be bigger.
    Let me explain what I want the code to do:
    Read to a array (BytesComentario) 42 bytes starting at index 11.

    Example: you have a file like this:
    Code:
    2A 2A BC 00 00 00 01 02 05 AC 05 03 04 05 F4 AB 2B 3F 9C
    And I want to read 5 bytes starting on index 9 to a array of bytes. So in that array I should get
    Code:
    05 03 04 05 F4
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [2005] FileStrem.Read

    The reason for the error is that you are setting an offset of eleven in the array then trying to fill the array with 42 elements so the array would have to be 41 + 11 like VladC# says but that would give you 11 empty elements at the start of the array which you dont want. I think what you need to do is to use Seek or Position to set the position in the stream to start reading from and just use no offset.

    Casey.

  5. #5

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] FileStrem.Read

    Thks a lot I used position and it worked. I've tried to used it before and also seek but didnt work, now I found why i still used the offset.

    But by the description they give on the offset my code should work.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

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