输出文件写入数组到同一行
Output file writing array to the same line
我目前正在尝试将 2d int 数组写入文件。我已经将数据写入文件,但它们都出现在同一行上。
目前是这样显示的:
15101219816911171076171411881213567131441415101069101413878101181210156976139188711571210891267910
但我希望它看起来像这样:
15,10,12,19,8
16,9,11,17,10
7,6,17,14,11
8,8,12,13,5
6,7,13,14,4
1,4,15,10,10
6,9,10,14,13
8,7,9,10,11
8,12,10,15,6
9,7,6,13,9
18,8,7,11,5
7,12,10,8,9
12,6,7,9,10
这是我的代码。
{
StreamWriter outputfile;
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
}
outputfile.Close();
}
}
Any help with this problem would be much appreciated. Thanks.
你应该先创建一个字符串写入文件,然后一次性写入
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
sb.Append(Global_Stock[i , j]);
sb.Append(",")
}
}
File.WriteAllText("ClosingStock.Txt",sb.ToString());
只需在每个内循环后添加一个新行:
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
outputfile.Write(Environment.NewLine);
}
如果我不得不猜测你应该在你的循环中切换 StockColumns
和 StockRows
以获得你想要的输出。
另外,请参阅@Mihir Dave 以更好地实现将字符串写入文件。
{
StreamWriter outputfile;
string comma = "";
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(comma);
outputfile.Write(Global_Stock[i , j]);
comma = ",";
}
comma = System.Environment.NewLine);
}
outputfile.Close();
}
我目前正在尝试将 2d int 数组写入文件。我已经将数据写入文件,但它们都出现在同一行上。
目前是这样显示的:
15101219816911171076171411881213567131441415101069101413878101181210156976139188711571210891267910
但我希望它看起来像这样:
15,10,12,19,8
16,9,11,17,10
7,6,17,14,11
8,8,12,13,5
6,7,13,14,4
1,4,15,10,10
6,9,10,14,13
8,7,9,10,11
8,12,10,15,6
9,7,6,13,9
18,8,7,11,5
7,12,10,8,9
12,6,7,9,10
这是我的代码。
{
StreamWriter outputfile;
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
}
outputfile.Close();
}
}
Any help with this problem would be much appreciated. Thanks.
你应该先创建一个字符串写入文件,然后一次性写入
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
sb.Append(Global_Stock[i , j]);
sb.Append(",")
}
}
File.WriteAllText("ClosingStock.Txt",sb.ToString());
只需在每个内循环后添加一个新行:
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(Global_Stock[i , j]);
}
outputfile.Write(Environment.NewLine);
}
如果我不得不猜测你应该在你的循环中切换 StockColumns
和 StockRows
以获得你想要的输出。
另外,请参阅@Mihir Dave 以更好地实现将字符串写入文件。
{
StreamWriter outputfile;
string comma = "";
File.WriteAllText("ClosingStock.Txt", string.Empty);
outputfile = File.AppendText("ClosingStock.Txt");
for (int i = 0; i < (StockColumns); i++)
{
for (int j = 0; j < (StockRows); j++)
{
outputfile.Write(comma);
outputfile.Write(Global_Stock[i , j]);
comma = ",";
}
comma = System.Environment.NewLine);
}
outputfile.Close();
}