简体   繁体   English

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

[英]C++ How to get substring from ifstream and getline()

What is printed to the console: 打印到控制台的内容:

START(0,0)
GOAL(0,2)
ooox
xxoo
ooox

I want to be able to obtain the substring of the START and GOAL points, not including the brackets just the coordinate pair. 我希望能够获得START和GOAL点的子字符串,不包括括号(仅包括坐标对)。 I would also want to store them as variables as well since I want to add validation whether the START or GOAL points are out of bounds from the grid. 我还想将它们存储为变量,因为我想添加验证,无论START或GOAL点是否超出网格范围。

I am trying to make an application to traverse the 2D grid, where the 'x' represents the blocked paths and 'o' represents unblocked. 我正在尝试制作一个遍历2D网格的应用程序,其中“ x”代表受阻路径,“ o”代表不受阻。

The starting point is always from the bottom left of the grid as represented below: 起点始终从网格的左下角开始,如下所示:

(0,2)(1,2)(2,2)(3,2)
(0,1)(1,1)(2,1)(3,1)
(0,0)(1,0)(2,0)(3,0)

I have tried using .substr() method with the start and end points of where I would like to store the values but it does not print out anything in the console. 我尝试将.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();
    }

I expect std::cout << startPoint << std::endl; 我期望std::cout << startPoint << std::endl; to print the substring into the console but it just reads the file and prints whatever is in it, and nothing else. 将子字符串打印到控制台中,但它只读取文件并打印其中的任何内容,而不打印其他任何内容。

I believe that getline only stores data from file into string line for every line of the file in the for loop until it reaches null. 我相信getline只会将文件中的数据存储到for循环中文件每一行的字符串行中,直到到达null为止。

So after the for loop line = null essentially. 因此,在for循环行之后本质上为null。

You need either an alternative way of reading the file or a way of storing the data for use outside the for loop scope(perhaps a string array). 您需要一种替代的方式来读取文件,或者需要一种存储数据的方式,以便在for循环范围(也许是字符串数组)之外使用。

Hope that helps :) 希望有帮助:)

The problem is you are reading ALL lines of the file first, THEN you are parsing only the last line that was read, asking for a starting index that is out of range. 问题是您先读取文件的所有行,然后仅解析最后读取的行,要求起始索引超出范围。

You need to move your parsing inside the reading loop instead: 您需要将解析移入阅读循环内:

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

Or, if you know the 1st two lines are ALWAYS START and GOAL : 或者,如果您知道第一行,则总是ALWAYS STARTGOAL

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

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

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