Read/Write Binary Files in VS C#

Creating Binary Files With C#

Page 1 of 1

2 Replies - 3224 Views - Last Post: 28 October 2010 - 12:32 PM Rate Topic: -----

#1 sisqo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-October 10

Read/Write Binary Files in VS C#

Posted 28 October 2010 - 12:06 PM

Hi there i'm making some exercises with Binary Files,
I've did this project to Create a Binary File and Writing in the Binary File:


using System;
using System.IO;

namespace binaryfiles
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"c:\pss\file1.bin";

            FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write);

            BinaryWriter bw = new BinaryWriter(fs);

            int nr, year;
            string firstname, lastname;

            nr = 1; 
            firstname = "Sisqo"; 
            lastname = "Williams"; 
            year = 2010;

            bw.Write(nr);
            bw.Write(firstname);
            bw.Write(lastname);
            bw.Write(year);
            
            bw.Close();
            fs.Close();            

        }
    }
}




This code worked perfectly, But i'm havin some trouble with another exercise and that is:

Creating a binary File with these data on it:

1 Sisqo Williams 2010
2 Michael Jordan 2010
3 Curtis Jackson 2010
4 Tom Cruise 2010

and then Creating an app that can do:
1.Read The Data That i've mentioned before
(nr,name,lastname & year)
2.Edit those data So
(when i call number 2 it has to show up the name Michael Jordan 2010,
and than i can edit it and replace michael jackson with another name
for example Barack Obama 2011 ?

I hope i was Clear (sorry for my english)

Can Anyone help me with this problem :)

Is This A Good Question/Topic? 0
  • +

Replies To: Read/Write Binary Files in VS C#

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,843
  • Joined: 02-June 10

Re: Read/Write Binary Files in VS C#

Posted 28 October 2010 - 12:25 PM

You posted the code from a previous project that works fine, but you didn't post the code for this project that you are having troubles with.

What exactly is the problem other than "doesn't work"?
Was This Post Helpful? 0
  • +
  • -

#3 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Read/Write Binary Files in VS C#

Posted 28 October 2010 - 12:32 PM

One (of many, and not the best) Solutions:

Try to read this message:
Youwereclearyourenglishwasverygood
It's possible, but not as easy as reading this message:
You were clear. Your English was very good.
The difference was the spaces and punctuation. We can use delimiters for the same purpose.

Right now your code writes the following to the file : 1SisqoWilliams2010. Your code will have a hard time figuring out where each part (nr, name, last, year) starts and stops. If you change it to : $1@Sisqo@Williams@2010#, it will be easier to figure out. The code can look for the $ to know it is the start of the new record. It will know each part is seperated by the @. It knows it reached the end of the record when it finds the #. Those symbols are arbitrary and made up. Use whatever character you want. You just have to be careful that the character isn't used in any of the data.

Note: I'm guessing this is for a class assignment so I didn't want to use an advanced technique, but there are better ways of doing this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1