如何将2d int数组中的数字写入文本文件中的确切位置?

时间:2014-08-14 03:09:04

标签: c# .net winforms

我有一个文本文件,这是内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Test
{
    class test1
    {
        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,},
};

我有一个代码,我从位图像素值创建2d int数组。

private void CreateArray(Bitmap bmp)
        {
            ret = new int[bmp.Width, bmp.Height];

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color color = bmp.GetPixel(x, y);

                    if (color.ToArgb() == Color.White.ToArgb())
                    {
                        ret[x, y] = 0;
                    }

                    else
                    {
                        ret[x, y] = 1;
                    }
                }
            }
        } 

现在,如果我像这样循环回来:

for (int k = 0; k < ret.GetLength(0); k++)
            {
                for (int l = 0; l < ret.GetLength(1); l++)
                {
                    var val = ret[k, l];
                }
            }

因此,一旦val将为1然后是1然后是1然后是1然后是0,0,0,0然后是1,1依此类推。 我需要从数组中取出这个0,1的数字,然后将它们写入文本文件中的0和1数字:

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[,] map = new int[,] 
    {
        {1,1,1,1,1,0,0,0,},
        {0,0,0,0,1,0,0,0,},
        {0,0,0,0,1,0,0,0,},
        {1,1,0,0,1,0,0,0,},
        {0,0,1,1,1,1,0,0,},
        {0,0,0,0,0,1,0,0,},
        {0,0,1,0,1,1,1,0,},
        {0,0,0,1,0,0,0,1,},
    };

我需要在文本文件中仅替换ret数组中的0和1数字。 也许我改为以某种方式替换写入ret数组,所以它将显示在文本文件中,就像我给出的例子一样。

但最终它应该如我在示例中所示。

修改

我有两个文本文件,我将稍后合并到一个文件。 第一个文本文件是顶部文件:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    namespace Test
    {
        class test1
        {
            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[,] 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,},
        };

只有数字0和1将被改变/替换为我在2d int数组变量ret中的数字。 我可以使用这样的空地图数组制作第一个文本文件:

int[,] map = new int[,] 
            {

            };

然后将ret中的数字0和1插入/添加到文本文件中,最后它将是这样的:

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

在任何情况下,我都需要保留文本域格式,就像我在示例中使用地图数组一样,只有0和1的数字会发生变化。

1 个答案:

答案 0 :(得分:-1)

我建议,使用正则表达式来隔离0,1行,然后对它们做一些工作, 然后覆盖文件,例如:

string[] Reader = File.ReadAllLines(sourceFile);
Regex 10line = new Regex(@"{\d,\d,\d,\d,\d,\d,\d,\d},")

foreach (string Line ind Reader) {
if 10line.isMatch(Line.trim());
doSomethingWith(Line);
}
writeNewFile();