c++从文件中读取以构建基于指针的迷宫

标签 c++ file pointers text graph

我正在尝试阅读“基于指针”的迷宫。每行的第一个字母对应房间的字母……然后后面的字母分别是北节点、东节点、南节点和西节点。星号表示空房间或无效选项。

所以这将是一个示例输入:

A E B * *
B * * * A
C G D * *
D * * * C
E I F A *
F J G * E
G K H C F
H L * * G
I * J E *
J * K F I
K * * G J
L * * H *

注释说:

We can't assume the rooms will be in order alphabetically A - Z, We are expecting a maximum of 12 rooms and there is a space between each letter or asterisk.

这是我目前所拥有的:

void Maze::read_maze(string FileName) {
    string line;
    ifstream inStream;
    inStream.open(FileName.c_str());
    int test = inStream.peek();
    int i = 0;
    if (!(inStream.fail())) {
        while (!inStream.eof() && test != EOF) {
            getline(inStream, line);
            Node n(line[0]);
            i++;
            rooms[i] = n;
            if (!(line[2] == '*')) {
                Node North = find_Node(line[2]);
                (find_Node(line[0])).set_North(&North);
            }
            if (!(line[4] == '*')) {
                Node East = find_Node(line[4]);
                (find_Node(line[0])).set_East(&East);
            }
            if (!(line[6] == '*')) {
                Node South = find_Node(line[6]);
                (find_Node(line[0])).set_South(&South);
            }
            if (!(line[8] == '*')) {
                Node West = find_Node(line[8]);
                (find_Node(line[0])).set_West(&West);
            }
        }
        currentRoom = find_Node('A');
    } else {
        cout << "Could not open the file, please choose another.\n" << endl;
        exit(1);
    }
}

当我开始解析文件时,我的程序会调用我拥有的函数来检查文件是否为空。所以我认为我读入的文件不正确。

最佳答案

您不能在程序运行时使用调试器来检查这些值吗?如果没有,那就试试

cost << line <<  endl; 

在getline之后,验证每行读入的实际数据。

关于c++从文件中读取以构建基于指针的迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200234/

相关文章:

c++ - C++中的文本编辑器?

c++ - Qt 中 _variant_t 的等价物

c++ - 在 std::find 方法中比较类对象

c++ - 是否有异步方式知道文件已更改?

java - 多线程写入文件时使用BufferReader读取文件

C - 将未初始化的变量传递给函数

c++ - 缓存刷新后的时间不确定性非常高

python - 使用python修改文本文档

c - 这个指针如何同时为空和不为空?

c - C 中的指针、malloc 和 char