【问题标题】:getline not working with opening a filegetline 无法打开文件
【发布时间】:2017-10-06 10:08:50
【问题描述】:

我正在打开一个文件并在一行中搜索一个关键字。 如果找到该关键字,我会继续从文件中读取行,直到再次在文件中找到相同的关键字。它在大多数情况下都有效。但在一种情况下,它无法正常工作。这一行

  ::MessageBox(NULL, TEXT("here -2"), TEXT(""), MB_OK);

当我检查时永远不会被执行。我假设 find 函数永远不会成功找到。我怀疑 getline() 函数。我正在查找的文本存在于文件中,它是带有空格的字符串[如“AB CD EF”]。它作为行上的第一个模式存在,并且行在此文本之前没有空格。任何人都可以提出我的错误做。我是 C++ 新手。这是我的代码。

std::ifstream in(curr_file_path);
std::string search("SEARCH");
std::string line;

char *pDestText = new char[8000*256+1];
int f_match = -1;
int l_match = -1;
int r_val=0;
int textLen = 0;
int flag = 0;
  while (std::getline(in, line))
    {

        r_val = line.find(search);
        if (r_val!=(std::string::npos))
        {
            ::MessageBox(NULL, TEXT("here -2"), TEXT(""), MB_OK);
            f_match = r_val;

            while (std::getline(in, line))
            {
                ::MessageBox(NULL, TEXT("here -3"), TEXT(""), MB_OK);
                r_val = line.find(search);
                if (r_val != std::string::npos)
                {
                    l_match = r_val;
                    break;
                }
                else
                {
                    ::MessageBox(NULL, TEXT("here -4"), TEXT(""), MB_OK);
                    for (int i = 0; i < line.size(); i++)
                    {
                        pDestText[textLen++] = line[i];
                    }
                }
            }
            ::MessageBox(NULL, TEXT("here -5"), TEXT(""), MB_OK);
            for (int i = 0; i < r_val; i++)
            {
                //if(line[i]!='=')
                ::MessageBox(NULL, TEXT("here -6"), TEXT(""), MB_OK);
                pDestText[textLen++] = line[i];
            }
            //pDestText[textLen] = '\0';
            ::MessageBox(NULL, TEXT("here -7"), TEXT(""), MB_OK);
            break;
        }
    }
    in.close();

【问题讨论】:

  • 当您使用调试器单步执行代码时,一次一行,因为它读取和解析文件,在每一步中轻松检查此处显示的所有变量的值,当有问题的代码读取包含您的搜索字符串的行时,您做了什么观察?没有理由坐等 stackoverflow.com 上的某个人为您解决问题,因为您本可以在五分钟内自己解决问题。您应该真正学习如何使用调试器。了解如何使用调试器是每个 C++ 开发人员的必备技能。这就是它的用途。
  • 山姆是 100% 正确的!如果你没有/使用调试器,至少使用一些调试打印,例如:你说你的东西 getline 不起作用,在它后面放一个 std::cout &lt;&lt; line &lt;&lt; std::endl 看看它读什么......

标签: c++ algorithm fstream getline


【解决方案1】:

我喜欢以简洁的方式向人们展示标准 C++¹。

Live On Coliru

#include <fstream>
#include <string>
#include <iostream>
#include <iterator>
#include <sstream>

// start SEARCH block

bool filter_lines(std::istream& haystack, std::string const& needle, std::ostream& dest) {
    bool active = false;
    std::string line;

    auto trigger = [&] { 
        bool match = std::string::npos != line.find(needle); 
        if (match)
            active = !active;
        return match;
    };

    auto act = [&] { if (active) dest << line << "\n"; };

    while (getline(haystack, line))
        if (!trigger())
            act();

    return active;
}

int main() {
    std::ifstream ifs("main.cpp");
    std::stringstream destText;

    if (filter_lines(ifs, "SEARCH", destText)) 
        std::cerr << "Warning: last delimited block unclosed\n";

    std::cout << "------------- FILTERED -----------\n" 
              << destText.rdbuf() 
              << "----------------------------------\n";
}

¹我意识到我可能无法解决确切的问题,但我觉得这种类型的答案通常更有益。建设性的 cmets 使用调试器来发现自己的错误也是如此

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2018-11-30
    相关资源
    最近更新 更多