0

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 {

1
  • The way that the line information is being written is a bit chaotic to say the least, try doing it in a more structured and logical / maintainable way, e.g. w.Write("{"); before you even enter the loop and see if there is indeed any information to write is your issue. Commented Aug 14, 2014 at 9:39

5 Answers 5

2

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

Also, Please use using statement. Don't manually code the Stream/StreamWriter yourself.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

Comments

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

That should do it.

Comments

0

Just to add to the pile of answers a bit of LINQ fun.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.