如何确定 c++ 中文件何时更新?
我正在使用一个应用程序,该应用程序将文件 X 作为输入并提供文件 Y 和 Z 作为输出(图形和绘图)
现在,如果应用程序再次运行:
- 文件 Y 和 Z 已经存在并且文件 X 未更新,则不应覆盖它。
- 文件 X 已更新,则文件 Y 和 Z 必须被覆盖。
代码是用 C++ 编写的,我如何更改代码才能满足这两个条件?我应该每次都取X的时间戳吗?
I am using an application which takes File X as input and gives File Y and Z as output (graphs and plots)
Now if the applicatoin is run again :
- File Y and Z already exists and File X is not updated then it should not be over-written.
- File X is updated, then File Y and Z must be overwritten.
The code is written in c++, how can i change the code such that both the conditions are met? Should I take time stamp of X everytime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用的是 POSIX 计算机,请使用
stat
或相关函数,例如fstat
。 Windows 等效项在 http://msdn 中进行了描述。 microsoft.com/en-us/library/14h5k7ff(v=vs.80).aspx。stat
返回的结构或 Windows 对应项有一个字段,表示文件上次修改的时间。If you are on a POSIX machine, use
stat
or a related function such asfstat
. The windows equivalents are described at http://msdn.microsoft.com/en-us/library/14h5k7ff(v=vs.80).aspx.The structure returned by
stat
or the windows counterparts have a field that represents the time at which the file was last modified.对于便携式解决方案,Boost.Filesystem 有 last_write_time 以
std::time_t
形式返回上次修改时间。For a portable solution, Boost.Filesystem has last_write_time that returns the time of last modification as a
std::time_t
.您可以只保留文件 X 的哈希值。如果发生更改,则该文件已更改(更新),您将需要更新 Y 和 Z。
You could just keep a hash of file X. If the has changes, the file has been changed (updated), and you will need to update Y and Z.
使用 David Hammen 的答案,比较 X 与 Y 和 Z 的修改时间戳,如果 X 的修改晚于 Y 或 Z - 那么它们已经过时,应该重写
using David Hammen's answer, compare modification timestamp of X against Y and Z, if X was modified later than Y or Z - so they are outdated and should be rewritten
是的,您应该检查文件 X 并查看(两个选项):
第一个是最简单且最快的。第二个是最准确的。
进一步编辑以下评论:这两个选项可以结合起来以达成妥协。
Yes, you should inspect the file X and see (two options):
The first is the easiest and the fastest. The second is the most accurate.
EDIT further to the comments below: These two options can be combined to reach a compromise.
另一种可能性:用 X 编写基本程序,总是覆盖 Y 和 Z。用 make 调用基本程序。一个类似于以下内容的 makefile
应该可以解决问题。然后您可以使用“make Y”或“make Z”来调用它
底线:让我们担心时间戳等问题。
Another possibility: write the basic program with X always overwriting Y and Z. Invoke the basic program with make. A makefile that looks like:
should do the trick. You can then invoke it with either "make Y" or "make Z"
Bottom line: let make worry about timestamps and such.
请注意,在检查上次更新时间时,有
Please note that when checking for last update time, there are some limitations: