Writing to Binary file

B

Bonzol

Hello!, I'm just trying to write my string to a simple binary file so
it can't be read in a text document,, however it doesn't work.. I do
this

FileStream fs = File.Create(Server.MapPath("secret.txt"));
BinaryWriter bw = new BinaryWriter(fs);

bw.Write("You should not be able to read me");

bw.Close();
fs.Close();

It writes to it and all fine, but when I open the text file I can read
it fine. However, integers are displayed as gibberish. so its half
working.

Any help?
Thanks in advance.
 
J

Jon Skeet [C# MVP]

Bonzol said:
Hello!, I'm just trying to write my string to a simple binary file so
it can't be read in a text document,, however it doesn't work.. I do
this

FileStream fs = File.Create(Server.MapPath("secret.txt"));
BinaryWriter bw = new BinaryWriter(fs);

bw.Write("You should not be able to read me");

bw.Close();
fs.Close();

Firstly, I'd suggest using "using" statements to automatically close
your streams/writers, even if exceptions occur.
It writes to it and all fine, but when I open the text file I can read
it fine. However, integers are displayed as gibberish. so its half
working.

Any help?

I think you've misunderstood the point of BinaryWriter. It's not meant
to make things unreadable - it's just meant to be an easy way of
writing all kinds of values which can be read with BinaryReader.

If you're trying to keep data secret, you should use encryption
instead.
 
B

Bonzol

Firstly, I'd suggest using "using" statements to automatically close
your streams/writers, even if exceptions occur.



I think you've misunderstood the point of BinaryWriter. It's not meant
to make things unreadable - it's just meant to be an easy way of
writing all kinds of values which can be read with BinaryReader.

If you're trying to keep data secret, you should use encryption
instead.

Im not that concerned with keeping data a secret, just want it to
appear unreadable when opend in a text document, and apparently this
method is supposed to do that,, but its not.
 
J

Jon Skeet [C# MVP]

Im not that concerned with keeping data a secret, just want it to
appear unreadable when opend in a text document, and apparently this
method is supposed to do that,, but its not.

What makes you think it's supposed to do that? Look at the
documentation for the method - it's doing exactly what it's meant to.

Jon
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Bonzol said:
Im not that concerned with keeping data a secret, just want it to
appear unreadable when opend in a text document, and apparently this
method is supposed to do that,, but its not.

That is not the use of it, as Jon pointed out it only intend to be able to
read/write any kind of binary data., if your data is only text most probably
it will write it like that. Do this, open an EXE in notepad and if you look
around you will see plain text around. Well the same happen in your case
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Bonzol said:
Im not that concerned with keeping data a secret, just want it to
appear unreadable when opend in a text document, and apparently this
method is supposed to do that,, but its not.

The "binary thingy" is mostly relevant for numbers. Text is the
same in text files and binary files.

So text is readable in binary files (unless it is EBCDIC or
east asian encodings ...)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top