从文本文件中查找并提取数据

发布于 2025-01-08 17:23:19 字数 1132 浏览 3 评论 0原文

我正在尝试搜索文本文件并在标题后提取数据。但是,我遇到了一些迭代器问题,我不知道如何克服。

这是一个示例文本文件:

继电器状态
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

理想情况下,我想调用 LoadData; Something.LoadData("Relay States"); 并让它返回一个带有 {0,0,0,0,0,0,0,0,...} 的 std::vector。

template<typename T> std::vector<T> CProfile::LoadData(const std::string& name)
{
    std::ifstream ifs(FILE_NAME);
    std::vector<T> data;
    std::istreambuf_iterator<char> iit = std::istreambuf_iterator<char>(ifs);

    std::search(iit, ifs.eof(), name.begin(), name.end());
    std::advance(iit, name.size() + 1);

    T buffer = 0;
    for(ifs.seekg(iit); ifs.peek() != '\n' && !ifs.eof(); data.push_back(ifs))
    {
        ifs >> buffer;
        data.push_back(buffer);
    }

    return data;
}

据我了解,我的代码的主要问题是:

  • std::search 是一个不明确的调用,我将如何解决这个问题?
  • ifs.seekg(iit) 不合法,我该如何使 iit 成为有效的参数?

谢谢。

I'm tring to search a text file and extract the data after a heading. However, I've got some issues with iterators that I don't know how to overcome.

This is a sample text file:

Relay States
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

Ideally, I'd like to call LoadData<bool> something.LoadData("Relay States"); and have it return an std::vector with {0,0,0,0,0,0,0,0,...}.

template<typename T> std::vector<T> CProfile::LoadData(const std::string& name)
{
    std::ifstream ifs(FILE_NAME);
    std::vector<T> data;
    std::istreambuf_iterator<char> iit = std::istreambuf_iterator<char>(ifs);

    std::search(iit, ifs.eof(), name.begin(), name.end());
    std::advance(iit, name.size() + 1);

    T buffer = 0;
    for(ifs.seekg(iit); ifs.peek() != '\n' && !ifs.eof(); data.push_back(ifs))
    {
        ifs >> buffer;
        data.push_back(buffer);
    }

    return data;
}

From what I understand, the main problems with my code are:

  • std::search is an ambiguous call, how would I go about fixing this?
  • ifs.seekg(iit) is not legal, how would I go about making iit a valid argument?

Thanks.

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

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

发布评论

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

评论(2

等往事风中吹 2025-01-15 17:23:19

好吧,我认为你对 std::search 的参数问题

std::search(iit, ifs.eof(), name.begin(), name.end());

应该是

std::search(iit, std::istreambuf_iterator<char>(), name.begin(), name.end());

for 循环中的 ifs.seekg(iit) 行不好,因为 seeg 需要一些streampos 类型的偏移量不是迭代器。所以它应该是 ifs.seekg(0)

Well I think your args to std::search is problem

std::search(iit, ifs.eof(), name.begin(), name.end());

should be

std::search(iit, std::istreambuf_iterator<char>(), name.begin(), name.end());

as for line : ifs.seekg(iit) in for loop isn't good , since seekg expects some offset of type streampos not an iterator. so it should be ifs.seekg(0)

哥,最终变帅啦 2025-01-15 17:23:19

像这样的事情怎么样:

template<typename T> std::vector<T> CProfile::RealLoadData(std::istream &is)
{
    std::string line;
    std::vector<T> data;

    while (std::getline(is, line))
    {
        if (line.empty())
            break;  // Empty line, end of data

        std::istringstream iss(line);

        T temp;
        while (iss >> temp)
            data.push_back(temp);
    }

    return data;
}

template<typename T> std::vector<T> CProfile::LoadData(const std::string& name)
{
    std::string line;
    std::ifstream ifs(FILE_NAME);

    while (std::getline(ifs, line))
    {
        if (line == name)
        {
            // Found the section, now get the actual data
            return RealLoadData<T>(ifs);
        }
    }

    // Section not found, return an empty vector
    return std::vector<T>();
}

How about something like this:

template<typename T> std::vector<T> CProfile::RealLoadData(std::istream &is)
{
    std::string line;
    std::vector<T> data;

    while (std::getline(is, line))
    {
        if (line.empty())
            break;  // Empty line, end of data

        std::istringstream iss(line);

        T temp;
        while (iss >> temp)
            data.push_back(temp);
    }

    return data;
}

template<typename T> std::vector<T> CProfile::LoadData(const std::string& name)
{
    std::string line;
    std::ifstream ifs(FILE_NAME);

    while (std::getline(ifs, line))
    {
        if (line == name)
        {
            // Found the section, now get the actual data
            return RealLoadData<T>(ifs);
        }
    }

    // Section not found, return an empty vector
    return std::vector<T>();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文