Binary file I/O using methods of streamreader and streamwriter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Buchan

    Binary file I/O using methods of streamreader and streamwriter

    Hello,

    I wonder if anyone could help me.

    I'm using vb.NET and I'd like to read a binary file, byte by byte, and
    then write to another file (making a duplicate, identical file).

    I'd then like to modify the program to take the integer value of each
    byte and say, add or subtract an integer from it, with the result
    still being an integer from 0 to 255. Then write another file that is
    just a list of the integers (as a column of numbers).

    I think that vb6 allowed the use of GET and PUT, but I don't think
    vb.NET allows those anymore. I believe I have to use the methods of
    streamReader and streamWriter to accomplish the task, but I don't know
    how.

    Thanks,
    Dave
  • Herfried K. Wagner [MVP]

    #2
    Re: Binary file I/O using methods of streamreader and streamwriter

    * pdbuchan@yahoo. com (David Buchan) scripsit:[color=blue]
    > I wonder if anyone could help me.
    >
    > I'm using vb.NET and I'd like to read a binary file, byte by byte, and
    > then write to another file (making a duplicate, identical file).
    >
    > I'd then like to modify the program to take the integer value of each
    > byte and say, add or subtract an integer from it, with the result
    > still being an integer from 0 to 255. Then write another file that is
    > just a list of the integers (as a column of numbers).
    >
    > I think that vb6 allowed the use of GET and PUT, but I don't think
    > vb.NET allows those anymore. I believe I have to use the methods of[/color]

    In VB.NET, they are called 'FileGet' and 'FilePut'.
    [color=blue]
    > streamReader and streamWriter to accomplish the task, but I don't know
    > how.[/color]

    Alreadly had a look at the 'Read', 'ReadLine', 'Write', 'WriteLine'
    methods and their overloaded versions?

    --
    Herfried K. Wagner [MVP]
    <URL:http://dotnet.mvps.org/>

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Binary file I/O using methods of streamreader and streamwriter

      David,
      If you are operating strictly byte reads & writes all you need is Stream or
      FileStream.

      I would use something like:

      Dim inputPath As String
      Dim outputPath As String

      Dim input As New FileStream(inpu tPath, FileMode.Open,
      FileAccess.Read )
      Dim output As New FileStream(outp utPath, FileMode.Create ,
      FileAccess.Writ e)

      Dim count As Integer = 1024
      Dim buffer(count - 1) As Byte

      count = input.Read(buff er, 0, count)
      Do Until count = 0
      ' modify each byte in buffer here
      output.Write(bu ffer, 0, count)
      count = input.Read(buff er, 0, count)
      Loop

      input.Close()
      output.Close()


      StreamReader & StreamWriter are more useful when you want to translate a
      text stream to/from "primitive" values, such as Integer, Double, and String.

      BinaryReader & BinaryWriter are useful when you want to translate a binary
      stream to/from "primitive" values, such as Integer, Double, and String.

      Hope this helps
      Jay

      "David Buchan" <pdbuchan@yahoo .com> wrote in message
      news:d40b946c.0 405091307.576d1 b68@posting.goo gle.com...[color=blue]
      > Hello,
      >
      > I wonder if anyone could help me.
      >
      > I'm using vb.NET and I'd like to read a binary file, byte by byte, and
      > then write to another file (making a duplicate, identical file).
      >
      > I'd then like to modify the program to take the integer value of each
      > byte and say, add or subtract an integer from it, with the result
      > still being an integer from 0 to 255. Then write another file that is
      > just a list of the integers (as a column of numbers).
      >
      > I think that vb6 allowed the use of GET and PUT, but I don't think
      > vb.NET allows those anymore. I believe I have to use the methods of
      > streamReader and streamWriter to accomplish the task, but I don't know
      > how.
      >
      > Thanks,
      > Dave[/color]


      Comment

      • George L.

        #4
        Re: Binary file I/O using methods of streamreader and streamwriter


        If you want to copy a file you can use "File" class from System.IO
        E.x) File.Copy("Sour ceFileName", "DestFileNa me")

        Now, if you want to work with the bytes of a file you can use "FileStream "
        class
        from the same namespace.
        E.x) Dim FS as new Filestream("Fil eName",Mode)
        Dim Buffer() as byte

        'Get the bytes from file to a byte array
        redim Buffer(FS.Lengt h-1)
        FS.Read(Buffer, 0,Buffer.Length )

        'Do your stuff... :-)
        For i as int32=0 to Buffer.Length-1
        Buffer(i) = '...... Your code.
        next

        'Save the byte array and close the stream.
        FS.Position=0
        FS.Write(Buffer ,0,Buffer.lengt h)
        FS.Close

        I hope this works for you...
        George

        ? "David Buchan" <pdbuchan@yahoo .com> ?????? ??? ??????
        news:d40b946c.0 405091307.576d1 b68@posting.goo gle.com...[color=blue]
        > Hello,
        >
        > I wonder if anyone could help me.
        >
        > I'm using vb.NET and I'd like to read a binary file, byte by byte, and
        > then write to another file (making a duplicate, identical file).
        >
        > I'd then like to modify the program to take the integer value of each
        > byte and say, add or subtract an integer from it, with the result
        > still being an integer from 0 to 255. Then write another file that is
        > just a list of the integers (as a column of numbers).
        >
        > I think that vb6 allowed the use of GET and PUT, but I don't think
        > vb.NET allows those anymore. I believe I have to use the methods of
        > streamReader and streamWriter to accomplish the task, but I don't know
        > how.
        >
        > Thanks,
        > Dave[/color]


        Comment

        • David Buchan

          #5
          Re: Binary file I/O using methods of streamreader and streamwriter

          Thanks guys.

          FileStream looks like just the ticket.

          I appreciate all the responses.

          Dave

          Comment

          Working...