1

Possible Duplicate:
Why does BinaryWriter prepend gibberish to the start of a stream? How do you avoid it?

public static void saveFile(string path, string data)
{
    using (Stream fileStream = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.None)) 
    {
        using (BinaryWriter bw = new BinaryWriter(fileStream))
        {
            bw.Write(data);
        }
    }
}

However, everytime the method is called it adds the following 2 characters before writing: alt text . I'm saving it to a .txt if it makes any difference. Also, the string displays fine on the trace output. How do I fix this?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109

2 Answers2

3

BinaryWriter.Write(string) http://msdn.microsoft.com/en-us/library/system.io.binarywriter.write.aspx - Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.

spender
  • 117,338
  • 33
  • 229
  • 351
  • We have highlighters for that: http://awurl.com/nrftM7YoI – Ben Voigt Mar 08 '10 at 20:35
  • Is it just me, or are more people suspicious of URL shortening and similar (such as the url above)? That highlighting is a great idea, but no thanks... I like to know where I'm headed. – spender Mar 08 '10 at 21:32
0

If you're saving a string to a plain text file, use a StreamWriter instead of the BinaryWriter:

public static void saveFile(string path, string data) 
{
    using (Stream fileStream = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.None)) 
    { 
        using (StreamWriter sw = new StreamWriter(fileStream)) 
        { 
            sw.Write(data);
        }
    }
}
Adam Lear
  • 38,111
  • 12
  • 81
  • 101