如何读取映射文件(0和1)并将其存储到C++中的数组中?

How to read a map file(0s and 1s) and store it into array in C++?

提问人:Learning 提问时间:10/16/2021 更新时间:10/16/2021 访问量:240

问:

我想读取一个名为 maze 的文件.txt其中包含 0 和 1(指示是否被阻止),并将文件中的每个元素存储到名为 maze[17][17] 的二维数组中。

迷宫.txt就像:

11111111111111111
10000000000101001
10100111111001101
10101100001010101
10111010100000001
10000011011111001
11111000010001001
10110010000100101
10110100101110111
10000111000100001
10110011000100101
10110010000011101
10111001111110101
10110010000010001
10010111110101111
10010000000000001
11111111111111111

我搜索过的最接近的答案是使用加号(请参阅:从 c++ 中的文件读取到行尾?getline()istringstream())

但是,上述解决方案仅适用于我在它们之间放置空格时,例如:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1
1 0 1 0 0 1 1 1 1 1 1 0 0 1 1 0 1
1 0 1 0 1 1 0 0 0 0 1 0 1 0 1 0 1
1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 1 1 0 1 1 1 1 1 0 0 1
1 1 1 1 1 0 0 0 0 1 0 0 0 1 0 0 1
1 0 1 1 0 0 1 0 0 0 0 1 0 0 1 0 1
1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 1 1
1 0 0 0 0 1 1 1 0 0 0 1 0 0 0 0 1
1 0 1 1 0 0 1 1 0 0 0 1 0 0 1 0 1
1 0 1 1 0 0 1 0 0 0 0 0 1 1 1 0 1
1 0 1 1 1 0 0 1 1 1 1 1 1 0 1 0 1
1 0 1 1 0 0 1 0 0 0 0 0 1 0 0 0 1
1 0 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

我的代码:

void read_maze(int map[17][17]) {
    ifstream read_file("D:/maze.txt", ios::in);
    if (read_file.good()){
        string str;
        int i = 0;
        while (getline(read_file, str) {
            int j = 0;
            istringstream ss(str);
            int num;
            while (ss >> num)
            {
                map[i][j] = num;
                j++;
            }
            i++;
        }
    }
    for (int i = 0; i < 17; i++)
    {
        for (int j = 0; j < 17; j++)
        {
            cout << map[i][j];
        }
        cout << endl;
    }
    read_file.close();
}

输出就像原始迷宫.txt

那么,我应该怎么做才能在不修改其内容的情况下将迷宫 .txt 中的每个元素存储到数组中?

我相信可能有一些更简单的解决方案,但由于我是C++的新手,我找不到任何像我这样的类似情况。

希望有人能根据上面的代码提供详细的代码。 多谢!

C++ 数组 矩阵 IOTream

评论

1赞 nurettin 10/16/2021
你为什么不使用 char c;并调用嵌套循环?然后剩下的就是将 c 转换为整数,例如read_file >> c;num = c - '0';
0赞 Learning 10/16/2021
谢谢@nurettin,您的解决方案神奇地工作,对我来说非常清楚!看来我工作了几个小时的事情,在短短几秒钟内就被你解决了:)
0赞 nurettin 10/17/2021
实际上,这是20年前解决的,花了几个小时。

答:

0赞 Pepijn Kramer 10/16/2021 #1

你可以这样做:

#include <array>
#include <iostream>
#include <string>
#include <sstream>

std::istringstream file{
    "11111111111111111\n"
    "10000000000101001\n"
    "10100111111001101\n"
    "10101100001010101\n"
    "10111010100000001\n"
    "10000011011111001\n"
    "11111000010001001\n"
    "10110010000100101\n"
    "10110100101110111\n"
    "10000111000100001\n"
    "10110011000100101\n"
    "10110010000011101\n"
    "10111001111110101\n"
    "10110010000010001\n"
    "10010111110101111\n"
    "10010000000000001\n"
    "11111111111111111\n"
};

auto load(std::istream& is)
{
    // use std::array, you can actually return that from functions
    // and avoids the pain of having to work with double pointers
    // and manual memory managment.
    std::array<std::array<char, 17>, 17> map{};

    // loop over all rows in the map
    for (auto& row : map)
    {
        // loop over all the values in a row
        for (auto& value : row)
        {
            // read one value from the file
            is >> value;
        }

        // newlines will be skipped.
    }

    // and return your map (2d array)
    return map;
}

int main()
{
    // file is a stringstream now but
    // is easily replaced by a filestream
    // (stringstream just makes testing more easy)
    auto map = load(file);

    // just another range based for loop
    // to display the map we read from file.
    for (const auto& row : map)
    {
        for (const auto& value : row)
        {
            std::cout << value;
        }
        std::cout << "\n";
    }
}

评论

0赞 Learning 10/16/2021
感谢@Pepijn克莱默的回答,但对于像我这样的新手来说,这似乎仍然很困难!
0赞 Tianshi 25 10/16/2021 #2

您的代码只需稍作修改即可工作。喜欢这个:

#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

void read_maze(int map[17][17]) {
    ifstream read_file("maze.txt", ios::in);
    if (read_file.good()){
        string str;
        int i = 0;
        while (getline(read_file, str)) {
            // read line and process each digit
            for (int j=0; j < str.size(); j++) {
                map[i][j] = str[j] - '0';
            }
            i++;
        }
    }
    for (int i = 0; i < 17; i++)
    {
        for (int j = 0; j < 17; j++)
        {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    read_file.close();
}

int main() {
    int map[17][17] = {};
    read_maze(map);
}

评论

0赞 hivert 10/17/2021
最好解释出了什么问题并显示更正,而不是仅仅丢弃一大堆更正的代码。