简体   繁体   English

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

[英]How do I create specific text format in a text file?

The format I need to create/write to the text file is this: 我需要创建/写入文本文件的格式是这样的:

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,},
};

This is the code I'm using now to create it: 这是我现在用来创建它的代码:

            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();
        }

What I get so far in the end in the text file is this: 到目前为止,我在文本文件中得到的结果是:

{,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},
{

It's almost fine but how do I rid/remove the last { ? 几乎可以,但是如何删除/删除最后一个{ The result should be for now: 结果应该是现在:

{,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},

I and then later to add the rest the }; 我然后再添加其余的}; in the end and the rest I will add later. 最后,其余的我会在后面补充。 But first only this part how to remove the last { 但首先只有这部分如何删除最后一个{

It can be much simpler: 它可以简单得多:

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("},");
    }
}

Here's Ideone sample 这是Ideone示例

Also, Please use using statement . 另外,请使用using语句 Don't manually code the Stream/StreamWriter yourself. 不要自己亲自编写Stream / StreamWriter。

Check if you have reached the end of your 2-D array before adding the extra brace bracket, like so: 在添加额外的花括号之前,请检查是否已到达二维数组的末尾,如下所示:

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

You can simplify the code like this: 您可以像这样简化代码:

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("},");
}
// 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();

That should do it. 那应该做。

Just to add to the pile of answers a bit of LINQ fun. 只是为了增加答案,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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM