从文本文件读入数组
我正在尝试将用 Vim 编辑的文本文件读入数组中。
文本文件大小为 30*50,由个位数组成。我一直在疯狂地尝试让它工作,但我认为由于换行符而出现了问题。这是我一直在使用的:
Map::Map(char* filename)
{
grid[30][50] = (0);
string line;
ifstream m_file(filename);
if (m_file.is_open())
{
while(m_file.good())
{
for (int i = 0; i < 30; i++)
{
getline(m_file,line);
for (int k = 0; k < 50; k++)
{
int tnum = atoi(line.c_str());
grid[i][k] = tnum;
}
}
}
m_file.close();
}
};
网格在头文件中定义为 int grid[30][50]。
我用来打印的代码如下:
void display_room(int trid[30][50])
{
for (int i = 0; i < 30; i++)
{
for (int k = 0; k < 50; k++)
{
mvprintw(i,k,"%d",trid[i][k]);
};
};
};
调用 Map sMap = Map("testmap"); 之后
我只是想将个位数捕获到数组中,然后重新打印该数组(使用curses)。目前,它读取测试映射文件,并打印全零,无论测试映射文件中有什么。
I'm trying to read a textfile that I've edited with Vim into an array.
The textfile is 30*50 and is composed of single digit numbers. I've been going crazy trying to get it to work, but I think I'm having issues due to newline characters. Here's what I've been using:
Map::Map(char* filename)
{
grid[30][50] = (0);
string line;
ifstream m_file(filename);
if (m_file.is_open())
{
while(m_file.good())
{
for (int i = 0; i < 30; i++)
{
getline(m_file,line);
for (int k = 0; k < 50; k++)
{
int tnum = atoi(line.c_str());
grid[i][k] = tnum;
}
}
}
m_file.close();
}
};
grid is defined in the header file as int grid[30][50].
The code I use to print is as follows:
void display_room(int trid[30][50])
{
for (int i = 0; i < 30; i++)
{
for (int k = 0; k < 50; k++)
{
mvprintw(i,k,"%d",trid[i][k]);
};
};
};
after calling Map sMap = Map("testmap");
I'm simply trying to capture the single digit numbers into an array, and reprint that array (using curses). Currently, it reads the testmap file, and prints all zeros, no matter what is in the testmap file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解你的问题:你的解析设置了整行的值,其中只有一个数字应该是...
将数字(ASCII 到 int/byte/... 可以通过这种方式完成:(
也许一些转换是需要。)
If I understand Your problem: Your parsing sets the value from the entire line where only a digit should be...
Translating the digit (ASCII to an int/byte/... can be done in this way:
(Perhaps some casting is needed.)
在内部循环中,您每次都使用该行的完整内容调用
atoi
。由于该行有 50 个字符长,atoi
无法将其转换为int
(int
可表示的最大值是 2147483647,并且您的数字可能比那个大)。当atoi
失败时,它返回0
。您想要的是将行中的每个字符转换为
int
。像这样的东西:In the inner loop, you're calling
atoi
with the full content of the line each time. As the line is 50 character long,atoi
cannot convert it to anint
(the largest representable value by anint
is 2147483647, and your number is probably larger than that). Whenatoi
fails, it return0
.What you want is convert each character of the line into an
int
. Something like that:再看看你的代码。试着看看它实际上说了什么,而不是你希望它说什么。
你显然希望该行依次读取该行上的五十个数字中的每一个。但它并没有这么说。它尝试将整行转换为整数(并尝试执行五十次)。
由于您的数字是个位数,因此您实际上需要更简单的东西
通过说 line[k] 您每次循环都会得到不同的数字(因为每次循环 k 都会增加)。
- '0'
位只是将字符转换为整数的技巧。看看你能否弄清楚它是如何工作的。Look at your code again. Try to see what is actually says instead of what you hope it says
You clear want that line to read each of the fifty numbers on the line in turn. But it doesn't say that. It tries to turn the whole line into an integer (and tries to do that fifty times).
Since your numbers are single digits, you actually need something much simpler
By saying line[k] you will get a different digit each time round the loop (because k increases each time round the loop). The
- '0'
bit is just a trick to turn a character into an integer. See if you can work out how it works.