如何在文本文件中创建特定的文本格式?

时间:2014-08-14 09:31:45

标签: c# .net

我需要创建/写入文本文件的格式为:

int[,] map = new int[,] 
{
    {0,0,0,0,0,0,0,0,},
    {0,0,0,0,0,0,0,0,},
    {0,0,0,0,0,0,0,0,},
    {1,1,1,1,0,0,0,0,},
    {0,0,0,1,0,0,0,0,},
    {0,0,1,1,0,0,0,0,},
    {0,0,0,0,1,1,1,0,},
    {0,0,0,0,0,0,0,1,},
};

这是我现在用来创建它的代码:

            int count = 0;
            StreamWriter w = new StreamWriter(@"C:\Temp\test\test.txt");
            w.Write("{");
            for (int k = 0; k < ret.GetLength(0); k++)
            {
                for (int l = 0; l < ret.GetLength(1); l++)
                {
                    var val = ret[k, l];
                    w.Write("," + val);
                    count++;
                    if (count == 8)
                    {
                        w.Write("},");
                        w.WriteLine(string.Empty);
                        w.Write("{");
                        count = 0;
                    }
                }
            }
            w.Close();
        }

我到目前为止在文本文件中到底得到的是:

{,1,1,1,0,0,0,0,0},
{,0,0,1,0,0,0,0,0},
{,0,0,1,1,0,0,1,1},
{,0,0,0,1,0,0,1,0},
{,0,0,1,1,0,0,1,0},
{,0,0,1,0,0,0,1,0},
{,0,0,1,1,1,0,1,0},
{,0,0,0,0,1,1,1,0},
{

这几乎没问题,但我如何摆脱/删除最后{? 结果应该是现在:

{,1,1,1,0,0,0,0,0},
{,0,0,1,0,0,0,0,0},
{,0,0,1,1,0,0,1,1},
{,0,0,0,1,0,0,1,0},
{,0,0,1,1,0,0,1,0},
{,0,0,1,0,0,0,1,0},
{,0,0,1,1,1,0,1,0},
{,0,0,0,0,1,1,1,0},

我,然后稍后将其余的};添加到最后,其余的我稍后会添加。 但首先只有这部分如何删除最后{

5 个答案:

答案 0 :(得分:2)

它可以更简单:

using( StreamWriter w = new StreamWriter(@"C:\Temp\test\test.txt"))
{
    for (int k = 0; k < ret.GetLength(0); k++)
    {
        w.Write("{");
        for (int l = 0; l < ret.GetLength(1); l++)
        {
            var val = ret[k, l];
            w.Write(val + ",");
        }
        w.WriteLine("},");
    }
}

此处Ideone sample

另外,请使用using statement。不要自己手动编写Stream / StreamWriter代码。

答案 1 :(得分:0)

在添加额外支撑支架之前,检查是否已到达二维阵列的末端,如下所示:

if (count == 8)
                {
                    w.Write("},");
                    w.WriteLine(string.Empty);
                    if(k != ret.GetLength(0) - 1 && l != ret.GetLength(1)-1)
                    {
                     w.Write("{");
                    }
                    count = 0;
                }   

答案 2 :(得分:0)

您可以像这样简化代码:

StreamWriter w = new StreamWriter(@"C:\Temp\test\test.txt");
for (int k = 0; k < ret.GetLength(0); k++)
{
    w.Write("{");
    for (int l = 0; l < ret.GetLength(1); l++)
    {
        w.Write(ret[k,l]);
        if(l < ret.GetLength(1)-1)
            w.Write(",");
    }
    w.WriteLine("},");
}

答案 3 :(得分:0)

// First opening bracket
w.WriteLine("{");

for (int i = 0; i < ret.GetLength(0); i++)
{
    // each line starts with an opening bracket
    w.Write("{");
    for (int j = 0; j < ret.GetLength(1); j++)
    {
        w.Write( = ret[i, j];
        if (j<7) // all commas but for the last
            w.Write(","}
    }
    w.WriteLine("},") // at the end of the line, close and comma
}
w.WriteLine("}") // close top bracket
w.Close();

应该这样做。

答案 4 :(得分:0)

只是为了增加一些LINQ乐趣的答案。

var rows = map.Cast<int>().Select((m, i) => new { m, i }).ToLookup(o => o.i / map.GetLength(0), o => o.m);
var text = string.Join(Environment.NewLine, rows.Select(r => string.Format("{{ {0} }},", string.Join(",", r))));

File.WriteAllText("foo.txt", text);