C++ 在多次文件读取后,使用ifstream读取文件不起作用
因此,我为树莓pi编写了这个应用程序,用于处理RFID标签。 无论如何,关键是标签靠近传感器,我读取标签,然后使用ifstream打开一个包含标签和个人设置的文件 代码如下:C++ 在多次文件读取后,使用ifstream读取文件不起作用,c++,raspberry-pi,ifstream,C++,Raspberry Pi,Ifstream,因此,我为树莓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;
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;
}
bool-Config::isTagInConfig(std::string-tagID)
{
///打开humans.cfg文件并检查标记是否在其中。
std::ifstream文件(路径);
std::字符串str;
std::字符串分隔符=“\t”;
while(std::getline(文件,str))
{
///由于tagID始终是第一列,因此下一行
///始终严格获取tagID。
std::string file_tagID=str.substr(0,str.find(分隔符));
if(tagID==file\u tagID)
{
file.close();
返回true;
}
}
std::cout我只是遇到了一个类似的问题,在运行程序一段时间后,ifstream不再工作。在打开太多文件后,它似乎不再工作,尽管我也在调用file.close()。结果我在程序的其他地方有一个fopen(),而没有fclose()我会检查你打开文件的任何地方,确保它们也被关闭。你甚至不需要调用关闭,因为RAII是惊人的。我想这是一个系统的问题,C++代码看起来很好。该死。这解释了为什么它没有做任何事情。嗯,我有什么想法可以知道发生了什么?哦,到了。找出问题所在,你可以使用fstream中的部分(goot、eof、bad、fail),尽管这不会告诉你哪里出了问题。