简体   繁体   中英

Loading data into a 2D array from text file?

I am using C# in a console application.

I need to load data from a text file and load it into a 2d array.

This is what I tried, but when I try to print out the contents of what gets returned nothing gets printed.

public static int[,] LoadMap()
{
    const string path = @"1.txt";

    string[] fileLines = File.ReadAllLines(path);
    int[,] map = new int[fileLines.Length, 15];
    string line;
    for (int i = 0; i < fileLines.Length; ++i)
    {
        line = fileLines[i];
        for (int j = 0; j < line.Length; ++j)
        {
            map[i, j] = (int)(line[j] - '0');
        }
    }

    return map;
}

But when I hardcode the data like that, then everything gets displayed perfectly.

private static int[,] Map = new int[MapX, MapY]
{
            { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

The data in the text file looks like that :

0,0,0,1,1,1,1,1,1,1,1,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

Any help will be appreciated whether you fix what I tried or propose something completely different, thanks.

 string[] fileLines = File.ReadAllLines(path);
        int[,] map = new int[fileLines.Length,fileLines[0].Split(',').Length];
        for (int i = 0; i < fileLines.Length; ++i)
        {
            string line = fileLines[i];
            for (int j = 0; j < map.GetLength(1); ++j)
            {
                string[] split = line.Split(',');
                map[i, j] = Convert.ToInt32(split[j]);
            }
        }
        return map;
    }

You could use LINQ to parse the lines:

var lines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 25];
for (int i = 0; i < fileLines.Length; ++i)
{
    var data = lines[i].Split(',').Select(c => Convert.ToInt32(c)).ToList();
    for(int j =0; j<25; ++j)
       map[i,j] = data[j];
}
return map;

If you could use a jagged array instead of a 2D array, this becomes simpler:

public static int[][] LoadMap()
{
    return File.ReadLines(path)
             .Select(l => l.Split(',').Select(Convert.ToInt32).ToArray())
             .ToArray();
}

If your text file has commas separating the values, replace this line:

for (int j = 0; j < line.Length; ++j)

With:

for (int j = 0; j < line.Length; j += 2)

That's assuming your values will always be only 1 char long.

When the data comes in from the text file. If it is separated by a comma you can separate it using string.split. Then you load what you get into an array and access it like you normally would do an array. Like below:

  string[] lines = System.IO.File.ReadAllLines(@"path");
  foreach (string line in lines)
  {  
      string[] first= line.Split(comma);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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