473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Writing to Binary file

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.

Jun 21 '07 #1
4 1848
Bonzol <Bo****@hotmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 21 '07 #2
On Jun 21, 4:35 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Bonzol <Bon...@hotmail.comwrote:
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.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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.

Jun 21 '07 #3
On Jun 21, 7:40 am, Bonzol <Bon...@hotmail.comwrote:
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.
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

Jun 21 '07 #4
Hi,

"Bonzol" <Bo****@hotmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
On Jun 21, 4:35 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
>Bonzol <Bon...@hotmail.comwrote:
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.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

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
Jun 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
4
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
3
by: Romain | last post by:
Hello, I am writing out a binary file. I figured that the number "10" is automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are line feed and carriage return. I know it does...
6
by: DanielEKFA | last post by:
Hey there :) I was once told that the STL classes had member functions to write their data to disk and to restore that data. Searching google (and why are there no "stl" or "map" manpages?), it...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.