Sharon Harel
Sharon Harel

Reputation: 1

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 {

Upvotes: 0

Views: 77

Answers (5)

Ilya Kozhevnikov
Ilya Kozhevnikov

Reputation: 10432

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

Upvotes: 0

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

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.

Upvotes: 2

Noctis
Noctis

Reputation: 11763

// 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.

Upvotes: 0

DavidG
DavidG

Reputation: 118937

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

Upvotes: 0

shree.pat18
shree.pat18

Reputation: 21757

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

Upvotes: 0

Related Questions