BUG: StreamWriter dislikes the "{" character

  • Thread starter Thread starter Chris Tacke, eMVP
  • Start date Start date
C

Chris Tacke, eMVP

I think I've found a bug. Put the code below in a Button click handler or
wherever. The Write function will cause an exception. The full framework
exhibits the same behavior.

-Chris

------------------
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.FileStream fs;
System.IO.StreamWriter sw;

fs = new System.IO.FileStream(@"\x.html", System.IO.FileMode.OpenOrCreate &
System.IO.FileMode.Truncate, System.IO.FileAccess.Write);
sw = new System.IO.StreamWriter(fs);

sb.Append("this is text");
sb.Append("\n");
sb.Append("{"); // exception caused by this
//sb.Append("\{"); // this prevents an exception, but it puts the (unwanted)
slash in the string
sb.Append("this is more text");

sw.Write(sb.ToString(), 0, 1);

sw.Flush();
sw.Close();
 
Chris,
fs = new System.IO.FileStream(@"\x.html", System.IO.FileMode.OpenOrCreate &
System.IO.FileMode.Truncate, System.IO.FileAccess.Write);

FileMode is not a Flags enum, so you can't combine multiple values,
you have to pick either OpenOrCreate or Truncate.

And if you were able to combine values, you should use the | operator,
not &.

sb.Append("{"); // exception caused by this
//sb.Append("\{"); // this prevents an exception, but it puts the (unwanted)
slash in the string
sb.Append("this is more text");

sw.Write(sb.ToString(), 0, 1);

This calls TextWriter.Write(string,object,object) since that method
isn't overridden by StreamWriter. The docs for that Write overload
says that is uses String.Format semantics for writing the string, i.e.
{ is treated as a formatting character. If you want { in your string,
you should escape it as {{. So

sb.Append("{{");

should work.



Mattias
 
I think I've found a bug. Put the code below in a Button
click handler or wherever.
fs = new System.IO.FileStream(@"\x.html",
System.IO.FileMode.OpenOrCreate &
System.IO.FileMode.Truncate, System.IO.FileAccess.Write);

Hmmmmm... FileMode is not a bitmask...

FileMode.OpenOrCreate = 4
FileMode.Truncate = 5

Do a binary AND on the two, and you're left with... 4!

That doesn't make sense.
sb.Append("this is text");
sb.Append("\n");
sb.Append("{"); // exception caused by this
sb.Append("this is more text");

sw.Write(sb.ToString(), 0, 1);

The (string, object, object) overload of TextWriter.Write
is quite clearly documented. I don't see a bug. "{this is
more text" is an invalid format specifier.

Maybe you should stick with eVB.
 
Maybe you should stick with eVB.
I would imagine that a lot of people on this newsgroup are genuinely pleased
that Chris did NOT stick with eVB. He ran the forums, when the CF was just
being released, that became the "place to go" when looking for CF related
information (http://www.innovativedss.com/forums/). And he's also a strong
player in the OpenNETCF movement (http://www.opennetcf.org/). Without
Chris's help on the newsgroup(s) a lot of people would have had a very
difficult time learning the nuances of the CF as well as creating their
CF-based apps.
Just to throw in my opinion (for anyone who cares).
 
Quite true. I was guessing it was trying to format it, which is why it
confused me when adding a backslash removed the error. I would have thought
something like "the post below this \{is from a moron" would be just as
invalid as "but he's sooo {smart". So why exactly is the former seen as
valid but the latter as invalid?

-Chris
 
Back
Top