locked
Reading/Writing to binary file??? RRS feed

  • Question

  • Apologize for this simple one (I'm learning each day :-)) but I am looking for the simplest way to create a binary file and write dat to it then read dat back in C# on WM6 smartphone device???

    Thanks in Advance

    Tuesday, April 7, 2009 5:05 PM

Answers

  • Yes, the BinaryWriter writes the length too where required. You just need to write/read in the same order and via the same data types.
    http://blog.voidnish.com
    • Marked as answer by Devsupport Tuesday, April 7, 2009 10:39 PM
    Tuesday, April 7, 2009 7:40 PM
    Moderator

All replies

  • File.ReadAllBytes and File.WriteAllBytes would be the simplest way to do this assuming they are available on the smartphone device you are targeting.
    http://blog.voidnish.com
    Tuesday, April 7, 2009 5:09 PM
    Moderator
  • I realize my question was somewhat vague!

    Would this be best for writing a hundred records?
     
    Below is how I am looking to do it BUT I run into problems when the first arg (name or age) is not a byte array???

    String name = "Bob";
    int  age = 35;

    File.Write(name, 0, 255);
    File.Write(age, 0, 2);

    Thanks again


    Tuesday, April 7, 2009 5:50 PM
  • Since you seem to be writing typed data, why do you need a binary file? Is that a design requirement?
    http://blog.voidnish.com
    Tuesday, April 7, 2009 5:55 PM
    Moderator
  • I need to keep it somwhat difficult to manually edit.
    Tuesday, April 7, 2009 6:18 PM
  • Instead of trying to write to the file yourself, use binary serialization via the BinaryFormatter  class. Obviously since your data itself is editable text, people may still be able to make changes if they want to but it won't be that easy.

    You should also look at some basic encryption - that might be a more apt solution for you.

    http://blog.voidnish.com
    Tuesday, April 7, 2009 6:22 PM
    Moderator
  • Woooo! That zoomed right over my head (I looked up the binary formatter)
    I guess I just want to see why my read and writes above will not work???
    Tuesday, April 7, 2009 6:58 PM
  • The File class does not have a static Write method. So your code will not compile at all.

    Or are you using some custom class of the same name?
    http://blog.voidnish.com
    Tuesday, April 7, 2009 7:06 PM
    Moderator
  • Here's how you'd do this via the BinaryWriter (since you don't want to go the serialization route) :

    String name = "Bob";
    int age = 35;

    using (BinaryWriter bw = new BinaryWriter(File.Create(@"<your path here>")))
    {
        bw.Write(name);
        bw.Write(age);
    }


    http://blog.voidnish.com
    Tuesday, April 7, 2009 7:12 PM
    Moderator
  • Nishant,
    How would you read that file. [I have never used the BinaryWriter/Reader before]
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, April 7, 2009 7:20 PM
  • John,

    It has to be read back in the same order :

    using (BinaryReader br = new BinaryReader(File.Open(@"<path to file>", FileMode.Open)))
    {
        Console.WriteLine(br.ReadString()); // name
        Console.WriteLine(br.ReadInt32()); // age
    }
    http://blog.voidnish.com
    • Proposed as answer by JohnGrove Tuesday, April 7, 2009 7:29 PM
    Tuesday, April 7, 2009 7:25 PM
    Moderator
  • Thanks!
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, April 7, 2009 7:29 PM
  • Does it automatically know the length of name (Some internal delimiter)?
    i.e. when reading back how do I know that the first name is 3 chars then the next name 10 chars?
    Tuesday, April 7, 2009 7:36 PM
  • Yes, the BinaryWriter writes the length too where required. You just need to write/read in the same order and via the same data types.
    http://blog.voidnish.com
    • Marked as answer by Devsupport Tuesday, April 7, 2009 10:39 PM
    Tuesday, April 7, 2009 7:40 PM
    Moderator
  • Thank you for your help Nishant!
    Tuesday, April 7, 2009 10:39 PM