By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,187 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,187 IT Pros & Developers. It's quick & easy.

writing to a binary file

P: n/a
Good Day

I would like to write a password to a binary file. I am following along
in the book and examples I have found googling. However when I open
the file in notepad it looks like a text file. Can someone tell me what
I am doing wrong?

try
{
if (sfdCommon.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
System.IO.Stream fs = File.Create(sfdCommon.FileName);
System.IO.BinaryWriter bw = new BinaryWriter(fs);
char [] b;
b=this.txtPassWord.Text.ToCharArray();
bw.Write(b);
bw.Close();
fs.Close();
}
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show (ex.StackTrace);
}

cheers

Bob
Mar 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Bob Cummings wrote:
Good Day

I would like to write a password to a binary file. I am following along
in the book and examples I have found googling. However when I open
the file in notepad it looks like a text file. Can someone tell me what
I am doing wrong?

try
{
if (sfdCommon.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
System.IO.Stream fs = File.Create(sfdCommon.FileName);
System.IO.BinaryWriter bw = new BinaryWriter(fs);
char [] b;
b=this.txtPassWord.Text.ToCharArray();
[ I don't think you should convert it to the char array, u really should
use a byte array.]
bw.Write(b);
bw.Close();
fs.Close();
}
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show (ex.StackTrace);
}

cheers [Also, make sure you close bw, fs in case some exceptions happen, which
is not the case here.

Either use a finally block , or use using statement to enclose those blocks.
]

Bob

Mar 30 '06 #2

P: n/a
Bob Cummings wrote:
Good Day

I would like to write a password to a binary file.


In what format? What bytes do you want to be in your file when you've
written it? That should be the first thing to decide - after that, life
gets easier :)

Note that a "text file" is just a binary file which happens to contain
sensible text if you decode it using a suitable encoding.

Jon

Mar 30 '06 #3

P: n/a
Your code works fine, it just creates a binaryfile where all the bytes
happen to be valid ascii characters. You need to encrypt each byte
somehow before you write them out to make the file unreadable.

e.g. for encryption an average 5 year old could break, try replacing
bw.write(b) with:

for (int i=0; i < b.Length; i++)
{
char c = (char)((int)b[i] - 20);
bw.Write(c);
}

Mar 30 '06 #4

P: n/a
Anthony Brown wrote:
Your code works fine, it just creates a binaryfile where all the bytes
happen to be valid ascii characters. You need to encrypt each byte
somehow before you write them out to make the file unreadable.

e.g. for encryption an average 5 year old could break, try replacing
bw.write(b) with:

for (int i=0; i < b.Length; i++)
{
char c = (char)((int)b[i] - 20);
bw.Write(c);
}

Got it thanks so much. For some I thought when I used to do this in C++
it was unreadable when opening the file stream ios::binary. Thanks for
5 year old encryption algorithm. This is for a charity event for a
college radio station. The students have devised their own alphabet
system for storing the cds on the shelf (depending on how the stars line
up sometimes G and O are the same letter <grin>). So this will be
sufficiently secure I am sure.
Mar 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.