在C#中将矩阵写入txt文件
我在 C# 中执行此操作
TextWriter tw = new StreamWriter("SQL_U.txt");
for (int j = 0; j < matrix.ColumnCount; j++)
{
for (int i = 0; i < matrix.RowCount; i++)
{
tw.WriteLine(U[i, j] + " ");
}
}
tw.Close();
,但得到的
-0,233694952850896
-0,150092222507639
-0,208798702657929
-0,231787481025756
-0,174773485921403
-0,0407654849559224
0,221253448397395
0,186510884928728
0,260534246686463
0,187890851164781
0,0878923620975135
0,234198798909221
0,291676946994878
0,327785825667529
0,237572713217582
-0,0860652250783251
-0,0421504222854907
-0,00555780603925644
是
-0,233694952850896 -0,150092222507639 -0,208798702657929 -0,231787481025756
-0,174773485921403 -0,0407654849559224 .....
如何修复它?
I am doing this in c#
TextWriter tw = new StreamWriter("SQL_U.txt");
for (int j = 0; j < matrix.ColumnCount; j++)
{
for (int i = 0; i < matrix.RowCount; i++)
{
tw.WriteLine(U[i, j] + " ");
}
}
tw.Close();
but get
-0,233694952850896
-0,150092222507639
-0,208798702657929
-0,231787481025756
-0,174773485921403
-0,0407654849559224
0,221253448397395
0,186510884928728
0,260534246686463
0,187890851164781
0,0878923620975135
0,234198798909221
0,291676946994878
0,327785825667529
0,237572713217582
-0,0860652250783251
-0,0421504222854907
-0,00555780603925644
instead
-0,233694952850896 -0,150092222507639 -0,208798702657929 -0,231787481025756
-0,174773485921403 -0,0407654849559224 .....
How to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Write
而不是WriteLine
以防止开始新行。然后在每行完成后插入一个新行。我还更改了您的代码以使用
using
而不是调用Close
,以便即使出现异常,编写器也能正确处理。使用此方法,您还可以在每行末尾写入一个额外的空格。如果这是一个问题,您可能需要稍微修改代码以根据索引有条件地写入此空间。
Use
Write
instead ofWriteLine
to prevent starting a new line. Then insert a new line after each row is complete.I also changed your code to use
using
instead of callingClose
so that the writer is correctly disposed even if there is an exception.Using this method you also write an extra space at the end of each line. If this is a problem you might want to modify your code slightly to write this space conditionally depending on the index.
这是因为您使用的是
WriteLine
,而不是Write
It's because you are using
WriteLine
, notWrite