繁体   English   中英

多次读取文件后,ifstream无法读取文件

[英]Reading files with ifstream not working after several file reads

因此,我为树莓派编写了可与RFID标签一起使用的应用程序。 无论如何,关键是标签靠近传感器,我读取了标签,然后使用ifstream打开了一个包含标签和个人设置的文件。

这是代码:

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

该代码在最初的几个小时内运行良好,但过了一段时间后由于某种原因无法读取该文件...我不断收到消息,指出在配置文件中找不到标签ID。

我尝试搜索为什么会发生这种情况。 我以前没有'file.close()'行,但是最近更改了它,但该错误仍然存​​在。

知道我在做什么错吗?

感谢您的阅读!

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

暂无
暂无

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

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