我正在使用 StreamWriter
类将一些文本写入新创建的文本文件,但输出总是出奇地错误,这是我的代码:
StreamWriter sw = new StreamWriter("newtextfile.txt", false, Encoding.Unicode);
for (int d = 0; d < count; d++)
{
sw.WriteLine(d + '^' + str1[d] + '^' + str2[d] + '^' + str3[d]);
}
sw.Flush();
sw.Close();
输出如下(这是文件的开头):
65342STR1^STR2^STR3
65343STR1^STR2^STR3
...等等
我不知道为什么 WriteLine
会跳过 d + '\'
并写入那个随机数。
最佳答案
尝试使用"
:
sw.WriteLine(d + "^" + str1[d] + "^" + str2[d] + "^" + str3[d]);
像这样的表达
int d = 0;
var t = d + '^'; // int + char
t
的类型是int
,而不是string
。
在你的例子中,65342
(== 0xFF3E
) 出现是因为它是这个特殊的 ^
类字符(全角抑扬符)的 Unicode口音)。
关于c# - StreamWriter 错误地写入文本文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26040127/