나는이 문제에 대한 어떤 해결책도 적용하지 못했습니다. 예외는이 행에서 발생합니다. currentMap [row, col] = Int32.Parse (s); 내가 뭘 걸려 라하는 것은이 방법이 같은 수의 행을 저장하는 특정 파일을 전달할 수 있습니다 :Int32에서 값이 너무 크거나 작습니다. 텍스트 파일 읽기 및 파싱
1,1,1
1,0,1
1,1,1
내가 다음 각 번호가 INT에 저장할을 [,] 반환됩니다 currentMap. 사용중인 파일에 큰 숫자가 없습니다. 나는 내가 만들고있는 어레이의 크기가 옳다고 생각한다. 그래서 이것이 왜 작동하지 않는지 이해할 수 없다. 나는 자바에서 NextInt를 사용하여 유사한 것들을하는 데 익숙하지만 C#에 대한 대안을 찾지 못했습니다.
도움 주셔서 감사합니다.
private int[,] LoadMapArray(String filename)
{
int[,] currentMap;
int rows = 0;
int cols = 0;
StreamReader sizeReader = new StreamReader(filename);
using (var reader = File.OpenText(filename))
{
while (reader.ReadLine() != null)
{
string line = sizeReader.ReadLine();
cols = line.Length;
rows++;
}
}
currentMap = new int[rows,cols];
StreamReader sr = new StreamReader(filename);
for (int row = 0; row < rows + 1; row++)
{
string line = sr.ReadLine();
string[] split = new string[] {","};
string[] result;
result = line.Split(split, StringSplitOptions.None);
int col = 0;
foreach (string s in result)
{
currentMap[row, col] = Int32.Parse(s);
col++;
}
}
return currentMap;
}
편집 : 파일 액세스 방법을 변경 한 후 코드가 수정되었습니다. 그런 다음이 값을 null로 설정해야합니다.
for (int row = 0; row < rows + 1; row++)
{
string line = sr.ReadLine();
string[] split = new string[] { "," };
string[] result;
if (line != null)
{
result = line.Split(split, StringSplitOptions.None);
int col = 0;
foreach (string s in result)
{
currentMap[row, col] = Int32.Parse(s);
col++;
}
}
}
중단되었을 때 s 값이 무엇인지 확인하기 위해 디버깅을 시도 했습니까? – 3dd
예외 세부 정보를 제공해 주시겠습니까? – BayStallion
디스크에있는 파일의 크기는 어느 정도입니까? – Steve