Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary File is text?

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
GB
Hi all, i am attempting to write some simple data to a binary file, this works but the data is readable here is the code i am using.

Code:
char delimiter = '*';
FileStream fileStream = new FileStream(dateFileName, FileMode.OpenOrCreate, FileAccess.Write);

string[] contents = new string[1] {"24/04/2007 00:00:00"};
BinaryWriter binaryWriter = new BinaryWriter(fileStream);

//put data into file
binaryWriter.Write(delimiter.ToString());
binaryWriter.Write(delimiter.ToString());
binaryWriter.Write(contents[0].ToString());

binaryWriter.Close();
fileStream.Close();
I get: **24/04/2007 00:00:00
But i expected to see unreadable characters. Does any body know why this is? Any help appreciated


Age is a consequence of experience
 
It doesn't matter whether it is a binary file or not, if you write string literals to the file, then you will have string literals in the file.

Depending on how you actually want the data to be stored, you would need to cast the strings accordingly.

Hope this helps.

[vampire][bat]
 
hmmm... ok thx

Age is a consequence of experience
 
Because you are using a BinaryWriter, what you are getting in the file is the string you wrote, preceded by a byte containing the number of characters in the string.

Try this instead:
Code:
            //put data into file
            binaryWriter.Write(delimiter);
            binaryWriter.Write(delimiter);
            binaryWriter.Write(contents[0].ToCharArray());
 
I see... thanks for that, I am at work at the moment but I will have a mess when I get home, unless I get chance to do it here later.
Again thx, I think that was the little push I needed :)

Age is a consequence of experience
 
I have tried ToCharArray(), still you see the given string rather the unreadable characters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top