从文本文件读入数组

发布于 2024-12-02 06:01:47 字数 1209 浏览 0 评论 0原文

我正在尝试将用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-12-09 06:01:47

如果我理解你的问题:你的解析设置了整行的值,其中只有一个数字应该是...

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

将数字(ASCII 到 int/byte/... 可以通过这种方式完成:(

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

也许一些转换是需要。)

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:

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

(Perhaps some casting is needed.)

梦亿 2024-12-09 06:01:47

在内部循环中,您每次都使用该行的完整内容调用 atoi 。由于该行有 50 个字符长,atoi 无法将其转换为 intint 可表示的最大值是 2147483647,并且您的数字可能比那个大)。当atoi失败时,它返回0

您想要的是将行中的每个字符转换为 int。像这样的东西:

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;
    }
}

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 an int (the largest representable value by an int is 2147483647, and your number is probably larger than that). When atoi fails, it return 0.

What you want is convert each character of the line into an 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;
    }
}
羁客 2024-12-09 06:01:47

再看看你的代码。试着看看它实际上说了什么,而不是你希望它说什么。

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

你显然希望该行依次读取该行上的五十个数字中的每一个。但它并没有这么说。它尝试将整行转换为整数(并尝试执行五十次)。

由于您的数字是个位数,因此您实际上需要更简单的东西

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

通过说 line[k] 您每次循环都会得到不同的数字(因为每次循环 k 都会增加)。 - '0' 位只是将字符转换为整数的技巧。看看你能否弄清楚它是如何工作的。

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). The - '0' bit is just a trick to turn a character into an integer. See if you can work out how it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文