首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >几次读取文件后,ifstream无法正常读取文件

几次读取文件后,ifstream无法正常读取文件
EN

Stack Overflow用户
提问于 2015-05-07 07:43:44
回答 1查看 278关注 0票数 1

因此,我为raspberry pi编写了这个应用程序,用于处理RFID标签。无论如何,关键是一个标签靠近传感器,我读取标签,然后使用ifstream打开一个包含标签和个人设置的文件。

代码如下:

代码语言:javascript
复制
bool Config::isTagInConfig(std::string tagID)
{
    /// Opens humans.cfg file and checks if tag is inside.
    std::ifstream file(PATH);
    std::string str;
    std::string delimiter = "\t";

    while (std::getline(file, str))
    {
        /// Since the tagID is always the first column, then the next line
        /// strictly obtains the tagID ALWAYS.
        std::string file_tagID = str.substr(0, str.find(delimiter));
        if (tagID == file_tagID)
        {
                file.close();
                return true;

        }
    }

    std::cout << tagID << " not found in Config file..." << std::endl;
    file.close();
    /// Consider adding a helper here, to add unknown tags.
    return false;
}

代码在开始的几个小时内运行良好,但过了一段时间后,由于某种原因,文件无法读取……我一直收到消息:在Config文件中找不到标签id。

我试着寻找为什么会发生这种情况。我以前没有'file.close()‘这一行,但是最近我改变了它,但是这个bug仍然存在。

知道我做错了什么吗?

感谢您的阅读!

EN

回答 1

Stack Overflow用户

发布于 2017-09-29 22:19:57

我刚刚遇到了一个类似的问题,在运行程序一段时间后ifstream不再工作。在打开了太多文件后,它似乎不再工作,即使我也在调用file.close()。原来我在程序的其他地方有一个fopen(),但没有使用fclose()。我会检查您正在打开的任何其他文件,并确保它们也被关闭。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30089527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档