- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我为树莓派编写了这个应用程序来玩转 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/
根据 C++11 标准,是 的行为 ifstream in("."); 指定的,还是系统相关的? 对于上下文,我试图避免使用 boost::filesystem 和类似的库,因为它们会导致不相关的可移
我尝试使用 ifstream 读取一个 3GB 的数据文件,但它给出了错误的文件大小,而当我读取一个 600MB 的文件时,它给出了正确的结果。除了错误的文件大小,我也无法使用 ifstream 读取
这里有没有人知道 C++ ifstream 的 get 指针在调用 read() 后可能会损坏的方法?我看到了一些我无法解释的真正奇怪的行为。例如(说明性代码,而不是我实际运行的代码): int ma
我正在编写一个外部合并排序。它是这样工作的:从大文件中读取 k 个 block ,在内存中对它们进行排序,执行 k 路合并,完成。所以我需要在 k-way 合并阶段从文件的不同部分顺序读取。最好的方法
阅读 Savitch 的 Problem Solving in C++,std::ifstream::fail 以检查文件是否已正确打开(ifstream 或 ofstream)。 正如我第一次看到的
我正在尝试逐行读取多个文件(本例中为 3 个)并使用 ifstream shared_ptrs vector 来执行此操作。但我不知道如何取消引用此指针以使用 getline() 或我的代码中存在其他
我正在编写一个程序,它从一个文本文件中获取多个变量。当程序发现EOF时, 它结束输入数据。 int main() { int val, count = 0; ifstream file
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想知道在什么情况下我们可以拥有: bool(std::ifstream) != std::ifstream::good() 区别在于 bool(std::ifstream) 不测试 eof 位,而
也许是个伪问题,但我需要一个明确的答案。这些函数的返回有什么不同吗 int FileExists(const std::string& filename) { ifstream file(file
我只是采用了一些在 Linux 下开发的文件阅读器的旧代码,并尝试在我的 Windows 项目中使用完全相同的代码,该项目是用 MSVC++7.1 编译的。代码编译没有任何问题,但根据 Windows
是var > var相同? 据我所知,它们应该完全相同。但是已经很晚了,我的大脑处于半睡状态,所以我想澄清一下。 最佳答案 它们不一样。 foo > foo是bar.operator>>(foo)或
我一直在测试使用 Visual Studio 在 C++ 中读取文件的性能,我得到了一些我真的不明白的结果。 我的代码如下: std::vector Method1(std::string fileP
我使用 SymbolHound 在网络上查找比较两者的资源,但找不到任何东西。 在 VS13 中寻找 std::ifstream::in 的声明和定义让我找到了 basic_ifstream。寻找 s
我无法将图像文件读入缓冲区。当我读取普通的 ascii 文件时,一切都很好,但当涉及到图像文件时,我怀疑图像文件中有一个\0 字符? 我需要接收图像文件,将其解析为 16KB 的 block 以进行哈
我知道已经有很多关于如何迭代 ifstream 的答案,但没有一个真正帮助我找到解决方案。 我的问题是:我有一个包含多行数据的txt文件。 txt 文件的第一行告诉我其余数据是如何组成的。例如这是我的
首先,我为我的英语表示歉意。 我在尝试编写一个 XML 解析器时遇到了一个奇怪的问题。为了解释我的问题,我应该说,我有一个 xml 解析器类,它有一个 ifstream 成员。这个类有一个函数,它会读
我正在使用 xcode。我编译得很好,然后一旦它运行它就成功打开文件,然后读取两个值,但这些值不会进入变量,因此它会跳过 for 循环并关闭文件并从 main 返回。 #include #inclu
我有一个包含以下格式数据的文件: name1 p1 p2 ... p11 name2 p1 p2 ... p11 ... (参数不一定在一行) 我的目标是读取名称和 11 个参数,对它们做一些事情,然
我正在尝试读取一个输入文件,该文件的格式为一行中有两个数字,并将该行中的第一个数字存储在一个 vector 中,将第二个数字存储在另一个 vector 中。 除了实际读取文件之外,我的代码的每个部分都
我是一名优秀的程序员,十分优秀!