473,671 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing to a binary file

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.Show Dialog() ==
System.Windows. Forms.DialogRes ult.OK)
{
System.IO.Strea m fs = File.Create(sfd Common.FileName );
System.IO.Binar yWriter bw = new BinaryWriter(fs );
char [] b;
b=this.txtPassW ord.Text.ToChar Array();
bw.Write(b);
bw.Close();
fs.Close();
}
}
catch(System.Ex ception ex)
{
System.Windows. Forms.MessageBo x.Show (ex.StackTrace) ;
}

cheers

Bob
Mar 30 '06 #1
4 1643
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.Show Dialog() ==
System.Windows. Forms.DialogRes ult.OK)
{
System.IO.Strea m fs = File.Create(sfd Common.FileName );
System.IO.Binar yWriter bw = new BinaryWriter(fs );
char [] b;
b=this.txtPassW ord.Text.ToChar Array();
[ 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.Ex ception ex)
{
System.Windows. Forms.MessageBo x.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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6413
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 points = 123 @@@begin data@@@ gibberish follows.....
6
23593
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
555
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 no way of knowing what the tables are, or what they contain. I am currently using the Dataset.WriteXML method passing an XMLWriter.
5
5554
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 integer arrays and the individual 1's and 0's are being written out as integers.
3
5488
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 that if the file is opened in something else than in binary mode. But in my example below, the file is really opened in binary.
6
5627
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 seems like someone was pulling my leg, because I really can't find anything about it. So, was I misinformed? Or perhaps this person was talking about some other classes you could use to do such a thing?
2
3064
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 array). I cannot change anything in the program but can add some features by which I can convert this array of data into a file. The easiest thing would be to write the data into a file (in hard disk) and use it. But I will be working on thousands...
3
18958
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 original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
6
5262
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
3806
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. Here is my part of my code. struct cust_data { int nCAFID; char *szFirstName;
0
8478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8397
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8599
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6230
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2813
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.