473,385 Members | 1,337 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,385 software developers and data experts.

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.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
4 1631
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
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
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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.