Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文本文件的内容存储到char[,]数组中_C#_Arrays_2d - Fatal编程技术网

C# 将文本文件的内容存储到char[,]数组中

C# 将文本文件的内容存储到char[,]数组中,c#,arrays,2d,C#,Arrays,2d,我试图得到一个坐标系,在这个坐标系中,调整x和y值将决定在哪里输出我的字符。例如,如果我的文本文件包含以下内容: 1 2 3 4 5 6 7 8 9 当我输出数组[2,3]=“X”时,我会得到 1 2 3 4 5 6 7 X 9 目前,我的数组只存储txt的第一行内容。我希望它显示文本中的值1到9。 我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S

我试图得到一个坐标系,在这个坐标系中,调整x和y值将决定在哪里输出我的字符。例如,如果我的文本文件包含以下内容:

1 2 3

4 5 6

7 8 9

当我输出数组[2,3]=“X”时,我会得到

1 2 3

4 5 6

7 X 9

目前,我的数组只存储txt的第一行内容。我希望它显示文本中的值1到9。 我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp2Test
{

public class Program

{
    public int xLocation;
    public int yLocation;

    public static void Main()
    {
        int counter = 0;
        string directory = System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString();
        directory += @"/Maps/Level1.txt";
        char[,] array1 = new char[3, 3];

        for (int i = 0; i < array1.GetLength(0); i++)
        {
            for (int j = 0; j< array1.GetLength(1); j++)
            {
                array1[i, j] = (Char)File.ReadAllBytes(directory)[counter];
                Console.Write(array1[i, j]);
                ++counter;
            }               
        }          
    }

}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
命名空间控制台测试
{
公共课程
{
公共场所;
公共场所;
公共静态void Main()
{
int计数器=0;
string directory=System.IO.directory.GetParent(System.IO.directory.GetParent(Environment.CurrentDirectory.ToString()).ToString();
目录+=@“/Maps/Level1.txt”;
字符[,]数组1=新字符[3,3];
for(int i=0;i

我做错了吗?

好吧,看起来你的文本比你的文本文件有更多的字符。您正在迭代3x3
array1
的每个元素,但文本文件有11个字符(空格也算在内)

以下是一种动态(但非常幼稚)的方法:

  • 使用而不是
    ReadAllBytes
  • 空白标记
  • 数组
  • 遍历每个符号并将其放入地图中
  • 资料来源:

    public static void Main()
    {
        string directory = System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString();
        directory = Path.Combine(directory, @"/Maps/Level1.txt"); // Better use Path.Combine for combining paths
    
        // It is enough to read the text once and not on every iteration
        string fileContent = File.ReadAllText(directory);
    
        // In your provided sample the blank sign signals a new row in the matrix
        string[] rows = fileContent.Split(' ');
    
        // Assuming that it is a matrix, which must always be the same width per line
        // NullChecks ...
        int rowLength = rows[0].Length;
        int rowCount = rows.Length;
        char[,] map = new char[rowLength, rowCount];
    
        for (int i = 0; i < rowCount; i++)
        {
            for(int j = 0; j < rowLength; j++)
            {
                map[i, j] = rows[i][j];
                Console.Write(map[i, j]);
            }
            // We are done with this row, so jump to the next line
            Console.WriteLine();
        }
    }
    
    publicstaticvoidmain()
    {
    string directory=System.IO.directory.GetParent(System.IO.directory.GetParent(Environment.CurrentDirectory.ToString()).ToString();
    directory=Path.Combine(directory,@/Maps/Level1.txt”);//最好使用Path.Combine来组合路径
    //只需阅读一次文本就足够了,而不是每次迭代
    字符串fileContent=File.ReadAllText(目录);
    //在你提供的样本中,空白符号在矩阵中发出新的行。
    string[]行=fileContent.Split(“”);
    //假设它是一个矩阵,每行的宽度必须始终相同
    //空支票。。。
    int rowLength=行[0]。长度;
    int rowCount=rows.Length;
    char[,]map=新字符[rowLength,rowCount];
    对于(int i=0;i
    能否提供一个示例
    Level1.txt
    ?目前,文本文件只是一组数字,如上图123 456 789所示,可能是