因此,我为raspberry pi编写了这个应用程序,用于处理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;
}代码在开始的几个小时内运行良好,但过了一段时间后,由于某种原因,文件无法读取……我一直收到消息:在Config文件中找不到标签id。
我试着寻找为什么会发生这种情况。我以前没有'file.close()‘这一行,但是最近我改变了它,但是这个bug仍然存在。
知道我做错了什么吗?
感谢您的阅读!
发布于 2017-09-29 22:19:57
我刚刚遇到了一个类似的问题,在运行程序一段时间后ifstream不再工作。在打开了太多文件后,它似乎不再工作,即使我也在调用file.close()。原来我在程序的其他地方有一个fopen(),但没有使用fclose()。我会检查您正在打开的任何其他文件,并确保它们也被关闭。
https://stackoverflow.com/questions/30089527
复制相似问题