Results 1 to 2 of 2

Thread: Binary file I/O (I hate you!)

  1. #1

    Thread Starter
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    Angry Binary file I/O (I hate you!)

    Has anyone had any luck with binary file I/O?

    I'm used to the good ole VB syntax...You know create a few structs for the headers and an array for the data. Then, read them in...It's like 6 lines (including the open and close).

    Actually this is about the same in C++. So, why is this so dang obscure in C#? Or am I just slow (this is altogether possible).

    I would like find an intelligent to read and write binary data (read not serialization) that is "Safe" in the .net sense. The biggest problem I've come accross so far is trying to read in a structure with what should be a fixed string:
    PHP Code:
    struct MakeBelieveStructure
    {
       public 
    string MyName;  // should be 20 characters
       
    public string MyAddress;  // should be 100 characters
       
    public int[] MyNumbers;  // List of 20 integers
    }
    // What I would like to do...
    BinaryReader  MyReader = new BinaryReader();
    MakeBelieveStructure MyStruct;
    MyStruct MyReader.Read(sizeof(MakeBelieveStructure)); 
    I've tried to explicitly define the structure, and to use pointers ( which work fine for fixed structure sizes ) but it just doesn't seem easy. And I haven't found a "Safe" way to do it yet....

    any ideas?
    -scott
    he he he

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    Re: Binary file I/O (I hate you!)

    this migh thelp

    PHP Code:
    The BinaryWriter and BinaryReader classes are used for writing 
    and reading datarather than character stringsThe following 
    code example demonstrates writing data to 
    and reading data 
    from a 
    new, empty file stream (Test.data). After creating the data 
    file in the current directory
    the associated BinaryWriter and 
    BinaryReader are created, and the BinaryWriter is used to write 
    the integers 0 through 10 to Test
    .datawhich leaves the file 
    pointer at the end of the file
    After setting the file pointer back to 
    the origin
    the BinaryReader reads out the specified content.

    [
    Visual Basic]
    Option Explicit
    Option Strict
    Imports System
    Imports System
    .IO
    Class MyStream
       
    Private Const FILE_NAME As String "Test.data"
       
    Public Shared Sub Main()
          
    ' Create the new, empty data file.
          If File.Exists(FILE_NAME) Then
             Console.WriteLine("{0} already exists!", FILE_NAME)
             Return
          End If
          Dim fs As New FileStream(FILE_NAME, FileMode.CreateNew)
          ' 
    Create the writer for data.
          
    Dim w As New BinaryWriter(fs)
          
    ' Write data to Test.data.
          Dim i As Integer
          For i = 0 To 10
             w.Write(CInt(i))
          Next i
          w.Close()
          fs.Close()
          ' 
    Create the reader for data.
          
    fs = New FileStream(FILE_NAMEFileMode.OpenFileAccess.Read)
          
    Dim r As New BinaryReader(fs)
          
    ' Read data from Test.data.
          For i = 0 To 10
             Console.WriteLine(r.ReadInt32())
             w.Close()
          Next i
       End Sub
    End Class
    [C#]
    using System;
    using System.IO;
    class MyStream {
       private const string FILE_NAME = "Test.data";
       public static void Main(String[] args) {
          // Create the new, empty data file.
          if (File.Exists(FILE_NAME)) {
             Console.WriteLine("{0} already exists!", FILE_NAME);
             return;
          }
          FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
          // Create the writer for data.
          BinaryWriter w = new BinaryWriter(fs);
          // Write data to Test.data.
          for (int i = 0; i < 11; i++) {
             w.Write( (int) i);
          }
          w.Close();
          fs.Close();
          // Create the reader for data.
          fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
          BinaryReader r = new BinaryReader(fs);
          // Read data from Test.data.
          for (int i = 0; i < 11; i++) {
             Console.WriteLine(r.ReadInt32());
             w.Close();
          }
       }
    }
    If Test.data already exists in the current directory, an IOException is thrown. Use FileMode.Create to always create a new file without throwing an IOException. 

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