比较 JSON::Value 变量中的数据,然后更新到文件
compare data in a JSON::Value variable and then update to file
我正在尝试通过在运行时提供文件名来将数据更新为两个 JSON 文件。
这是 updateTOFile 函数,它将存储在 JSON 变量中的数据更新为两个不同线程中的两个不同的数据。
void updateToFile()
{
while(runInternalThread)
{
std::unique_lock<std::recursive_mutex> invlock(mutex_NodeInvConf);
FILE * pFile;
std::string conff = NodeInvConfiguration.toStyledString();
pFile = fopen (filename.c_str(), "wb");
std::ifstream file(filename);
fwrite (conff.c_str() , sizeof(char), conff.length(), pFile);
fclose (pFile);
sync();
}
}
主题 1:std::thread nt(&NodeList::updateToFile,this);
话题 2:std::thread it(&InventoryList::updateToFile,this);
现在,即使上次执行没有数据更改,它也会更新文件。我想仅在与以前存储的文件相比有任何变化时才更新文件。如果没有变化,那么它应该打印数据是相同的。 任何人都可以帮忙吗? 谢谢。
您可以在编写之前检查它是否已更改。
void updateToFile()
{
std::string previous;
while(runInternalThread)
{
std::unique_lock<std::recursive_mutex> invlock(mutex_NodeInvConf);
std::string conf = NodeInvConfiguration.toStyledString();
if (conf != previous)
{
// TODO: error handling missing like in OP
std::ofstream file(filename);
file.write (conf.c_str() , conf.length());
file.close();
previous = std::move(conf);
sync();
}
}
}
然而,这种循环中的持续轮询可能是低效的。您可以添加Sleep
以使其不那么勤奋。另一种选择是通过NodeInvConfiguration
本身进行跟踪,如果它已更改并在存储时清除该标志。
相关文章:
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- cmake更新缓存的变量
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 更新到莫哈韦后出现cmath错误
- 在std::thread中,joinable()然后join()线程安全吗
- OpenMP:并行更新数组总是需要减少数组吗
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 指针没有更新它在void函数内部指向的值
- 如何在c++中获取要更新的值
- 已修改的LinkedList未在文本文件本身中更新
- Qt:当QListView获得新条目时,如何更新QStringList
- C++:如何读取分离变量,然后读取向量
- 比较 JSON::Value 变量中的数据,然后更新到文件
- 如何比较两个向量中的两个值,然后在C++中更新向量值
- 检查映射中是否存在关键字,然后更新值