所以我为树莓派编写了这个应用程序来玩转 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()。我会检查您打开文件的其他任何地方,并确保它们也已关闭。
关于c++ - 多次读取文件后,使用 ifstream 读取文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30089527/