C++ C++;如何从ifstream和getline()获取子字符串

C++ C++;如何从ifstream和getline()获取子字符串,c++,c++11,substring,getline,C++,C++11,Substring,Getline,打印到控制台的内容: START(0,0) GOAL(0,2) ooox xxoo ooox 我希望能够获得起始点和目标点的子字符串,不包括括号,只包括坐标对。我还希望将它们存储为变量,因为我希望添加验证,以确定起始点或目标点是否超出网格范围 我正在尝试制作一个应用程序来遍历2D网格,其中“x”表示阻塞的路径,“o”表示未阻塞的路径 起点始终从栅格的左下角开始,如下所示: (0,2)(1,2)(2,2)(3,2) (0,1)(1,1)(2,1)(3,1) (0,0)(1,0)(2,0)(3,0

打印到控制台的内容:

START(0,0)
GOAL(0,2)
ooox
xxoo
ooox
我希望能够获得起始点和目标点的子字符串,不包括括号,只包括坐标对。我还希望将它们存储为变量,因为我希望添加验证,以确定起始点或目标点是否超出网格范围

我正在尝试制作一个应用程序来遍历2D网格,其中“x”表示阻塞的路径,“o”表示未阻塞的路径

起点始终从栅格的左下角开始,如下所示:

(0,2)(1,2)(2,2)(3,2)
(0,1)(1,1)(2,1)(3,1)
(0,0)(1,0)(2,0)(3,0)
我尝试过使用.substr()方法,其起点和终点都是我想要存储值的地方,但它在控制台中没有打印出任何内容

void Grid::loadFromFile(const std::string& filename){
    std::string line;
    std::ifstream file(filename);
    file.open(filename);
    // Reads the file line by line and outputs each line
    while(std::getline(file, line)) {
        std::cout << line << std::endl;
    }

        std::string startPoint, goalPoint;

        startPoint = line.substr(6,3);
        std::cout << startPoint << std::endl;

        file.close();
    }
void Grid::loadFromFile(const std::string和filename){
std::字符串行;
std::ifstream文件(文件名);
打开(文件名);
//逐行读取文件并输出每一行
while(std::getline(文件,行)){

std::cout我认为getline只将文件中的数据存储到for循环中文件每一行的字符串行中,直到它达到null为止

因此,在for循环行之后,实际上等于null

您需要另一种读取文件的方法或存储数据的方法,以便在for循环范围外使用(可能是字符串数组)


希望有帮助:)

问题是您首先读取文件的所有行,然后只解析读取的最后一行,请求超出范围的起始索引

您需要将解析移到读取循环中:

void Grid::loadFromFile(const std::string& filename)
{
    std::ifstream file(filename);
    if (!file.is_open()) return;

    std::string line, startPoint, goalPoint;
    std::vector<std::string> grid;

    while (std::getline(file, line))
    {
        if (line.compare(0, 5, "START") == 0)
            startPoint = line.substr(6,3);
        else if (line.compare(0, 4, "GOAL") == 0)
            goalPoint = line.substr(5,3);
        else
            grid.push_back(line);
    }

    file.close();

    std::cout << startPoint << std::endl;
    std::cout << goalPoint << std::endl;

    // process grid as needed...
}

小心,在C++中,“null”一词完全是另一种意思(并且,既然<代码>行<代码>是<代码> STD::String 它永远不能为空)另外,循环后
line
中的值将是
getline
读取的最后一个值。当
getline
file
失败时停止,它不会触及
line
。这并不意味着实际空值意味着EOF,但我错了,当到达b/c行时,不会触及它。感谢您的帮助correction@scohe001 “当
getline
file
失败时停止,它不会触摸
line
”-不正确。
std::getline()
首先清除
std::string
,然后再尝试从流中读取。因此,失败时,
std::string
将为空。@RemyLebeau你完全正确。看起来我想的是流
>
操作符。好主意!顺便说一句,垃圾邮件
std::endl
是一种众所周知的性能-模式(尤其是在OP中)。
void Grid::loadFromFile(const std::string& filename)
{
    std::ifstream file(filename);
    if (!file.is_open()) return;

    std::string line, startPoint, goalPoint;
    std::vector<std::string> grid;

    if (!std::getline(file, line)) return;
    if (line.compare(0, 5, "START") != 0) return;
    startPoint = line.substr(6,3);

    if (!std::getline(file, line)) return;
    if (line.compare(0, 4, "GOAL") != 0) return;
    goalPoint = line.substr(5,3);

    while (std::getline(file, line))
        grid.push_back(line);

    file.close();

    std::cout << startPoint << std::endl;
    std::cout << goalPoint << std::endl;

    // process grid as needed...
}