简体   繁体   English

从文本文件读入数组

[英]Reading from text file into an array

I'm trying to read a textfile that I've edited with Vim into an array. 我正在尝试将用Vim编辑的文本文件读入数组。

The textfile is 30*50 and is composed of single digit numbers. 文本文件为30 * 50,由一位数字组成。 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]. grid在头文件中定义为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"); 在调用Map sMap = Map(“ testmap”);之后;

I'm simply trying to capture the single digit numbers into an array, and reprint that array (using curses). 我只是试图将单个数字捕获到一个数组中,然后(使用curses)重新打印该数组。 Currently, it reads the testmap file, and prints all zeros, no matter what is in the testmap file. 当前,无论测试图文件中有什么内容,它都会读取测试图文件并打印全零。

If I understand Your problem: Your parsing sets the value from the entire line where only a digit should be... 如果我了解您的问题:您的解析会从整行开始设置值,而整行应该只包含一位数字...

int tnum = atoi(line.c_str());
grid[i][k] = tnum;

Translating the digit (ASCII to an int/byte/... can be done in this way: 可以通过以下方式将数字(ASCII转换为int / byte / ...):

grid[i][k] = line[k] - '0';

(Perhaps some casting is needed.) (也许需要一些强制转换。)

In the inner loop, you're calling atoi with the full content of the line each time. 在内部循环中,您每次都使用该行的全部内容调用atoi As the line is 50 character long, atoi cannot convert it to an int (the largest representable value by an int is 2147483647, and your number is probably larger than that). 由于该行的长度为50个字符, atoi无法将其转换为intint可以表示的最大值是2147483647,并且您的数字可能大于该数字)。 When atoi fails, it return 0 . atoi失败时,它返回0

What you want is convert each character of the line into an int . 您需要将行的每个字符转换为一个int Something like that: 像这样:

for (int i = 0; i < 30; i++)
{
    getline(m_file,line);
    for (int k = 0; k < 50; k++)
    {
        // The ASCII character of the digits 0 to 9 have
        // successives values.
        int tnum = line[k] - '0';
        grid[i][k] = tnum;
    }
}

Look at your code again. 再次查看您的代码。 Try to see what is actually says instead of what you hope it says 尝试查看实际所说的内容,而不是您希望的内容

int tnum = atoi(line.c_str());

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 由于您的数字是个位数,因此您实际上需要简单得多的东西

int tnum = line[k] - '0';

By saying line[k] you will get a different digit each time round the loop (because k increases each time round the loop). 通过说line [k],您将在每次循环中得到一个不同的数字(因为k每次循环都增加)。 The - '0' bit is just a trick to turn a character into an integer. - '0'位只是将字符转换为整数的一个技巧。 See if you can work out how it works. 看看您是否可以知道它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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