【发布时间】:2012-01-04 23:02:54
【问题描述】:
我目前正在制作游戏,但我似乎无法从文本文件中读取值。出于某种原因,当我读取该值时,它给了我该值的 ASCII 码,而不是我将其写入文件时的实际值本身。我已经尝试过所有的 ASCII 转换函数和字符串转换函数,但我似乎无法弄清楚。
我使用一个二维整数数组。我使用嵌套的 for 循环将每个元素写入文件。我查看了文件并且值是正确的,但我不明白为什么它返回 ASCII 码。这是我用来写入和读取文件的代码:
写入文件:
for (int i = 0; i < level.MaxRows(); i++)
{
for (int j = 0; j < level.MaxCols(); j++)
{
fileWrite.Write(level.GetValueAtIndex(i, j) + " ");
//Console.WriteLine(level.GetValueAtIndex(i, j));
}
//add new line
fileWrite.WriteLine();
}
这是我从文件中读取值的代码:
string str = "";
int iter = 0; //used to iterate in each column of array
for (int i = 0; i < level.MaxRows(); i++)
{
iter = 0;
//TODO: For some reason, the file is returning ASCII code, convert to int
//keep reading characters until a space is reached.
str = fileRead.ReadLine();
//take the above string and extract the values from it.
//Place each value in the level.
foreach (char id in str)
{
if (id != ' ')
{
//convert id to an int
num = (int)id;
level.ChangeTile(i, iter, num);
iter++;
}
}
这是我用来读取值的循环的最新版本。读取其他值很好;只是当我到达阵列时,事情就出错了。我想我的问题是,为什么会发生向 ASCII 的转换?如果我能弄清楚这一点,那么我也许可以解决这个问题。我正在使用 XNA 4 制作我的游戏。
【问题讨论】: