Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi I want to create a file which contains both text data and binary data. Which is the best possible way to create this. CStdioFile provided by MFC allows to achieve the same. But in C# which is the equivalent class for this or some other class which provides the same functionality.

the data should be like this

File output:

//text data- human readable
PersonName : Rama
Age : 22

//binary data
  @I@-@    Qi*@    >÷À    ØŽ!@   @S›%À   àíÔÀ   ÀÕ†@   à÷ß*À   à0’*@   à@   @wB@   €Ð-@    ¡ÙÀ    -øâ?   à¿	À    \ÑÀ   €ÔÞ@   ÀÉ"À    l@   @÷¤@   €¼RÀ    ¤@    ÆÈÀ    Ý¤@   €}?

[edit]Spurious code block removed - OriginalGriff[/edit]
Updated 1-Aug-11 22:27pm
Praveen Kullu 2-Aug-11 5:28am    
Even the MSWord document (.doc & .docx) contains text and binary data.

I advise you to use the classes System.IO.BinaryWriter and System.IO.BinaryReader (and not stream classes). They can read and write both binary and text data. Note that all data is essentially binary. Text data just a sort of binary.

То write or read data, use overloaded methods Write or Read for each data type, including string.

Now, about the text. Pay attention that the writer and reader constructors allow to specify encoding. This is important, because the text can be written in different encoding. Make sure encodings match when you write and read the same data.


This is how your code sample should look:

using (
    System.IO.BinaryWriter writer = new System.IO.BinaryWriter(
                  double[] binary = new double[] { 123, 456, 8, 9, 0, 7, 6, 5, 5, 4, 3, 3 };
                  writer.Write("Line 1");
                  writer.Write("Line 2");
                  writer.Write("Line 3");
                  foreach (double val in binary)

Check out "using". It is important as it guarantees closing of streams even if exception occurs.

Share this answer
Raghaav 3-Aug-11 7:40am    
Thanks reply. I tried with suggested approach. But the data which is supposed to written in binary format is not written in binary format. It is written in ascii. FileStream aFileStream = new FileStream(@"c:\\welcometostream_codeproject.rda", FileMode.Create, FileAccess.Write, FileShare.None);

StreamWriter writer = new StreamWriter(aFileStream);

double[] binary = new double[] { 123, 456, 8, 9, 0, 7, 6, 5, 5, 4, 3, 3 };

writer.Write("Line 1");
writer.Write("Line 2");
writer.Write("Line 3");

foreach (double val in binary)

Raghaav 3-Aug-11 7:45am    
the array data i was expecting to write in the binary format
Sergey Alexandrovich Kryukov 3-Aug-11 12:20pm    
Sorry, I messed up things; give mew a minute, I'll fix it. Thanks for the note.
Sergey Alexandrovich Kryukov 3-Aug-11 12:42pm    
Fixed. I added code sample and tested it. Sorry for the inconvenience and thanks for pointing out the problem.

If it (finally!) makes sense, please formally accept the solution (green button) -- thanks.
Raghaav 4-Aug-11 1:17am    
Thanks for the reply. As said earlier I wanted to the strings to be human readable. Now strings are writtten with special characters, which should not happen. To achieve this i changed the encoding to ascii. But still the special characters are seen. How it can be solved? Apart from this each string should be displayed in the seperate line. writer.(String.Format("Person Name: {0} \n", Data.PersonName)); the code mentioned in this comment can be used or any other approach should be used to write a string in seperate line.
How hard can that be? I googled "C# write binary file" and these two below are the very first:


Now if you are wondering how I came to think of these unlikely search terms, I must confess I don't have a clue. ;)


Share this answer
Raghaav 2-Aug-11 5:20am    
the output doesn't give the string hello, instead it has some special characters. which i don;t want
Manfred Rudolf Bihy 2-Aug-11 5:33am    
That is because Strings in .NET are unicode. So when you write to a binary file without converting the text to ascii first you'll see strange characters appear inbetween. Have a look at System.Text.Encoding.ASCII and the associated sample found here.

Raghaav 2-Aug-11 5:38am    
StreamWriter writer

= File.CreateText("C:\\temp.txt");

double[] binary = new double[] { 123, 456, 8, 9, 0, 7, 6, 5, 5, 4, 3, 3 };

writer.WriteLine("Line 1");

writer.WriteLine("Line 2");

writer.WriteLine("Line 3");


BinaryWriter writer1 = new BinaryWriter(writer.BaseStream);

writer1.Seek((int)writer.BaseStream.Length, SeekOrigin.Begin);

foreach(double val in binary)






This is one of the code which we came up, which suffices the need.

But i want to have one stream which handles like binarywriter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900