使用getline作为输入将Fstream作为输入的问题

发布于 2025-01-22 09:39:58 字数 2325 浏览 2 评论 0原文

请原谅我以前发布过。我尝试搜索此问题,但无法描述我的问题。

我现在正在从事一个项目,这使我们在图中找到连接。该程序可以接受终端或输入文件的用户输入。我创建了一个驱动程序函数,该函数将ISTream作为参数,该参数取决于用户是从终端输入命令还是从输入文件中传递它们。

我认为我的问题来自“ getline”。我使用getline,因为其中一些顶点的名称中有一个空间。当我使用CIN时,该程序有效,但在FSTREAM上存在问题。

该程序根据用户的输入执行不同的搜索。如果我将getline与CIN一起使用,则该程序可以理解命令,但它与Getline和fstream不起作用。如果我使用>>将fstream传递到字符串。操作员,没有问题,但是读取搜索查询的getline命令之后以空白的形式出现。我还尝试过Cin.ignore()无济于事。

这是所讨论的功能:

void six_degrees::start(string filename, istream &input, ostream &output_type)
{
    populate(filename);
    string command;
    string source;
    string dest;

    while (input)
    {
        input >> command; // used to be a getline() but had issues reading when passing fstream

        if (command == "dfs")
        {
            input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // didn't work
            getline(input, source);
            getline(input, dest);
            if (database_check(source) && database_check(dest))
            {
                print_path(output_type, degrees.dfs(source, dest), source, dest);
            }
        }
        else if (command == "bfs")
        {
            getline(input, source);
            getline(input, dest);
            if (database_check(source) && database_check(dest))
            {
                print_path(output_type, degrees.report_path(source, dest), source, dest);
            }
        }
        else if (command == "not")
        {

            getline(input, source);
            getline(input, dest);
            string list;
            vector<Artist> exclude;

            while (list != "*")
            {
                getline(input, list);
                exclude.push_back(list); // uses getter to return artist instance
            }
            if (database_check(source) && database_check(dest))
            {
                if (exclusion_check(exclude))
                {
                    print_path(output_type, degrees.not_search(source, dest, exclude), source, dest);
                }
            }
        }
        else if (command == "quit")
        {
            break;
        }
        else
        {
            cout << command << " is not a command. Please try again" << endl;
        }
    }
}

Please excuse me if this has been posted before. I tried searching for this but couldn't describe my problem.

I am working on a project right now that has us finding connections in a graph. The program can either accept user input from the terminal or from an input file. I have created a driver function that takes an istream as a parameter which is dependent on whether or not the user is inputting commands from the terminal or passing them in from an input file.

I think my problem is coming from "getline". I use getline because some of the vertices have names with a space in them. The program works when I use cin, but has problems with fstream.

The program executes different searches depending on the user's input. If I use getline with cin, the program can understand the commands but it does not work with getline and fstream. If I pass the fstream to a string using the >> operator, there's no issue, but the getline commands that read the search queries come up as blanks after that. I've also tried cin.ignore() to no avail.

Here's the function in question:

void six_degrees::start(string filename, istream &input, ostream &output_type)
{
    populate(filename);
    string command;
    string source;
    string dest;

    while (input)
    {
        input >> command; // used to be a getline() but had issues reading when passing fstream

        if (command == "dfs")
        {
            input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // didn't work
            getline(input, source);
            getline(input, dest);
            if (database_check(source) && database_check(dest))
            {
                print_path(output_type, degrees.dfs(source, dest), source, dest);
            }
        }
        else if (command == "bfs")
        {
            getline(input, source);
            getline(input, dest);
            if (database_check(source) && database_check(dest))
            {
                print_path(output_type, degrees.report_path(source, dest), source, dest);
            }
        }
        else if (command == "not")
        {

            getline(input, source);
            getline(input, dest);
            string list;
            vector<Artist> exclude;

            while (list != "*")
            {
                getline(input, list);
                exclude.push_back(list); // uses getter to return artist instance
            }
            if (database_check(source) && database_check(dest))
            {
                if (exclusion_check(exclude))
                {
                    print_path(output_type, degrees.not_search(source, dest, exclude), source, dest);
                }
            }
        }
        else if (command == "quit")
        {
            break;
        }
        else
        {
            cout << command << " is not a command. Please try again" << endl;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

毁我热情 2025-01-29 09:39:58

@Whozcraig弄清楚了。我正在使用引起该问题的Windows Powershell。我登录了使用Linux的远程服务器(无论如何它们都应该是),但没有问题。

@WhozCraig figured it out. I was using windows powershell which caused the issue. I logged into my schools remote server that uses linux (where they're supposed to be anyway) and didn't have a problem.

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