19

Well I'm trying to write some values and strings to a text file.
but this text file must contain 2 bytes

These are the 2 bytes I want to insert to my text file after finishing writing the other values to it:

hex

I tried this method but I have no idea how to write bytes through it

using (StreamWriter sw = new StreamWriter(outputFilePath, false, Encoding.UTF8)) 

I have no idea about how to write them to the text file after putting the strings I want on it.

4
  • 1
    Have you tried File.Open? Commented Oct 15, 2013 at 21:10
  • It's not clear what these bytes are meant to mean, or what the rest of the text file is. Can you give us more context? Commented Oct 15, 2013 at 21:19
  • 1
    Are you trying to write UTF-16 text? Those two bytes may be the Byte Order Mark (BOM) for UTF-16 (see en.wikipedia.org/wiki/Byte_order_mark#UTF-16). In which case, try changing to Encoding.Unicode in your call, and I believe the framework will write the BOM for you. Commented Oct 15, 2013 at 22:20
  • possible duplicate of Save from SQL table to a text file Through Data Table and stream writer Commented Oct 16, 2013 at 13:56

6 Answers 6

31

I just figured this out. It works quite well for me. The idea is you open the file with a FileStream that can write byte arrays, and put a StreamWriter on top of it to write strings. And then you can use both to mix strings with your bytes:

StreamWriter writer = new StreamWriter(new FileStream("file.txt", FileMode.OpenOrCreate)); byte[] bytes = new byte[] { 0xff, 0xfe }; writer.BaseStream.Write(bytes, 0, bytes.Length); 
Sign up to request clarification or add additional context in comments.

1 Comment

.BaseStream is what was needed to get under StreamWriter and send raw bytes
17

If I recall correctly from your question. You want to write strings to a file and then write bytes to it?

This example will do that for you:

using (FileStream fsStream = new FileStream("Bytes.data", FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(fsStream, Encoding.UTF8)) { // Writing the strings. writer.Write("The"); writer.Write(" strings"); writer.Write(" I"); writer.Write(" want"); writer.Write("."); // Writing your bytes afterwards. writer.Write(new byte[] { 0xff, 0xfe }); } 

When opening the "Bytes.data" file with a hex editor you should see these bytes: enter image description here

1 Comment

This worked fine, except that I had to use writer.Write(Encoding.UTF8.GetBytes("foobar)); to prevent a SUB char at the beginning of the file. (SUB is what notepad++ showed as first character...)
6

Here is one more way to look for a solution...

StringBuilder sb = new StringBuilder(); sb.Append("Hello!! ").Append(","); sb.Append("My").Append(","); sb.Append("name").Append(","); sb.Append("is").Append(","); sb.Append("Rajesh"); sb.AppendLine(); //use UTF8Encoding(true) if you want to use Byte Order Mark (BOM) UTF8Encoding utf8withNoBOM = new UTF8Encoding(false); byte[] bytearray; bytearray = utf8withNoBOM.GetBytes(sb.ToString()); using (FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Request.MapPath("~/" + "MyFileName.csv"), FileMode.Append, FileAccess.Write)) { StreamWriter sw = new StreamWriter(fileStream, utf8withNoBOM); //StreamWriter for writing bytestream array to file document sw.BaseStream.Write(bytearray, 0, bytearray.Length); sw.Flush(); sw.Close(); fileStream.Close(); } 

Comments

4

If I understand correctly, you're trying to write some strings to a text file, but you want to add 2 bytes to this file.

Why won't you try using: File.WriteAllBytes ?

Convert your string to a Byte array using

byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str); // If your using UTF8 

Create a new byte array from the original byteArray with the additional 2 bytes.

And write them to a file using:

File.WriteAllBytes("MyFile.dat", newByteArray) 

Comments

0

There is a StreamWriter.Write(char) that will write a 16-bit value. You should be able to set your variable with the hex value like char val = '\xFFFE' and pass it to Write. You could also use FileStream where all the Write methods work off bytes, and it specifically has a WriteByte(byte) method. The MSDN documentation for it gives an example of outputting UTF8 text.

Comments

-1

After saving the string simply write those bytes using for example using File.WriteAllBytes or a BinaryWriter: Can a Byte[] Array be written to a file in C#?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.